0

I have created buttons to click and run python/open file.

import subprocess
import sys
import os
import tkinter
from tkinter import messagebox
top=tkinter.Tk()
class object_1:
    def __init__(self, G, M, L, R): 
        ## G is the .py script
        ## M is the manual
        ## L is the location
        ## R is the botton column
        self.G = G
        self.M = M
        self.L = L
        self.R = R
    def script(self):
        ## Change the working folder
        os.chdir(self.L)
        ## Run the .py 
        os.system(self.G)
    ## Create button for running .py 
    def script_run(self):
        script_obj = tkinter.Button(text = self.G, command = lambda:self.script())
        script_obj.grid(row = self.R, column = 0)  
    def manual(self):
        ## Change the working folder
        os.chdir(self.L)
        ## Open the .pptx manual
        subprocess.call(('cmd', '/C', 'start', '', lambda:self.M)) 
    ## Create button for opening .pptx manual
    def manual_run(self):
        manual_obj = tkinter.Button(text=self.M,command= lambda:self.manual(), bg='grey') 
        manual_obj.grid(row = self.R, column = 1) 
    def run_all(self):
        self.script_run()
        self.manual_run()
# These three should be combined together
script_value = ['Script1',
                'Script2']
manual_value = ['Script1_PPT.pptx', 
                'Script2_PPT.pptx']
location_value = ['C:\Python34\Location1',
                  'C:\Python34\Location2']
# loop through three locations, every scripts has its own location, even if they are the same
for i in range(2):
    GUI_Class = object_1(script_value[i], manual_value[i], location_value[i], i)
    GUI_Class.run_all()
  1. Actually, script_value,manual_value,location_value are three different properties for one script. How should I combine them together in one dictionary instead of separated them in three dictionary?
  2. For tkinter.button, if "command" can not be performed, e.g. there is no corresponding file exist, how to control the button stay there without any function or print a warning information?

Thank you!

Zong
  • 1,319
  • 1
  • 8
  • 6
  • http://stackoverflow.com/q/5767228/7432 – Bryan Oakley Dec 01 '16 at 23:43
  • Thank you for you remark, Bryan! The attached existing question has resolved my major problem by using lambda. There are only 2 remaining small issue with the script. Any further suggestion for improvement is welcoming! – Zong Dec 04 '16 at 05:52

0 Answers0