使用tkinter运行脚本并打开文件,在课堂上密封按钮

时间:2016-12-01 23:35:48

标签: python dictionary tkinter

我创建了单击并运行python / open文件的按钮。

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. 实际上,script_value,manual_value,location_value是一个脚本的三个不同属性。我应该如何将它们组合在一个字典中,而不是将它们分成三个字典?
  2. 对于tkinter.button,如果"命令"无法执行,例如没有相应的文件存在,如何控制按钮停留在那里没有任何功能或打印警告信息?
  3. 谢谢!

0 个答案:

没有答案