仅在选中复选框后,如何显示文件内容?

时间:2019-03-08 08:37:35

标签: python python-3.x tkinter

在下面的给定代码中,仅当选中“ x”复选框时,才应启用“ ChoosexFile”按钮。仅当单击“ ChoosexFile”按钮时,才必须打开“文件对话框”。并且所选文件的内容必须显示在中间框架的“列表框”中。中间框架和底部框架的列表框必须同时包含“水平”和“垂直”滚动条。并且,当单击“清除”按钮(顶部框架)时,必须清除显示在中间框架“列表框”中的文件内容,并且必须自动取消选中其复选框(x或y)。必须对“ y”复选框重复相同的操作(即功能必须与“ x”相同)。并且,当单击中间框架的“重置”按钮时,必须清除“列表框”(中间框架)中显示的所有内容,并且所有复选框都必须自动取消选中。

from tkinter import *
from tkinter import filedialog

def forButton1():
    filename1 = filedialog.askopenfilename()

    with open(filename1) as f:
        for i in f:
            myList.insert(END, i)

    print(filename1)

def forButton2():
    filename1 = filedialog.askopenfilename()

    with open(filename1) as f:
        for i in f:
            myList.insert(END, i)

    print(filename1)

def forButton7():
    root.destroy()

root = Tk()
root.title("Spatialization of DSSAT")

root.grid_columnconfigure(0, weight=1)

topFrame = LabelFrame(root, text="Select input file")
topFrame.grid(row=0, column=0, padx=8, pady=8, sticky=N+E+S+W)
topFrame.grid_rowconfigure(0, weight=1)
topFrame.grid_rowconfigure(1, weight=1)
topFrame.grid_columnconfigure(0, weight=1)
topFrame.grid_columnconfigure(1, weight=1)
topFrame.grid_columnconfigure(2, weight=1)

middleFrame = LabelFrame(root, text="Input data")
middleFrame.grid(row=1, column=0, padx=8, pady=8, sticky=N+E+S+W)
middleFrame.grid_rowconfigure(0, weight=1)
middleFrame.grid_rowconfigure(1, weight=0)
middleFrame.grid_columnconfigure(0, weight=1)
middleFrame.grid_columnconfigure(1, weight=1)

bottomFrame = LabelFrame(root, text="Model Output")
bottomFrame.grid(row=2, column=0, padx=8, pady=8, sticky=N+E+S+W)
bottomFrame.grid_rowconfigure(0, weight=1)
bottomFrame.grid_columnconfigure(0, weight=1)

MyVar1 = IntVar()
MyVar2 = IntVar()

MyCheckbutton1 = Checkbutton(topFrame, text="x", variable=MyVar1)
MyCheckbutton1.grid(row=0, column=0, padx=4, pady=4)
Button1 = Button(topFrame, text="Choose xFile", command=forButton1)
Button1.grid(row=0, column=1, padx=4, pady=4)
Button3 = Button(topFrame, text="Clear")
Button3.grid(row=0, column=2, padx=4, pady=4)

MyCheckbutton2 = Checkbutton(topFrame, text="y", variable=MyVar2)
MyCheckbutton2.grid(row=1, column=0, padx=4, pady=4)
Button2 = Button(topFrame, text="Choose yFile", command=forButton2)
Button2.grid(row=1, column=1, padx=4, pady=4)
Button4 = Button(topFrame, text="Clear")
Button4.grid(row=1, column=2, padx=4, pady=4)

innerMiddleFrame = Frame(middleFrame)
innerMiddleFrame.grid(row=0, column=0, columnspan=2, padx=4, pady=4)
innerMiddleFrame.grid_columnconfigure(0, weight=1)
innerMiddleFrame.grid_columnconfigure(1, weight=0)

scrollbar = Scrollbar(innerMiddleFrame)
myList = Listbox(innerMiddleFrame, yscrollcommand=scrollbar.set)
myList.grid(row=0, column=0, sticky=N+E+S+W)
scrollbar.config(command=myList.yview)
scrollbar.grid(row=0, column=1, sticky=N+E+S+W)

Button5 = Button(middleFrame, text="Reset")
Button5.grid(row=1, column=0, padx=4, pady=4)

Button6 = Button(middleFrame, text="Submit")
Button6.grid(row=1, column=1, padx=4, pady=4)

innerBottomFrame = Frame(bottomFrame)
innerBottomFrame.grid(row=0, column=0, columnspan=2, padx=4, pady=4)
innerBottomFrame.grid_columnconfigure(0, weight=1)
innerBottomFrame.grid_columnconfigure(1, weight=0)

scrollbar = Scrollbar(innerBottomFrame)
myList = Listbox(innerBottomFrame, yscrollcommand=scrollbar.set)
myList.grid(row=0, column=0, sticky=N+E+S+W)
scrollbar.config(command=myList.yview)
scrollbar.grid(row=0, column=1, sticky=N+E+S+W)

Button7 = Button(bottomFrame, text="Exit", command=forButton7)
Button7.grid(row=6, column=0, padx=4, pady=4)

root.geometry("400x590")
root.mainloop()

1 个答案:

答案 0 :(得分:0)

以禁用状态开始按钮,并通过单击复选框来设置其状态切换功能。

Button1 = Button(topFrame, text="Choose xFile", command=forButton1, state="disabled")
Button2 = Button(topFrame, text="Choose yFile", command=forButton2, state="disabled")

...

def check_state(widget):
    if widget["state"] == "normal":
        widget["state"] = "disabled"
    else:
        widget["state"] = "normal"

MyCheckbutton1.config(command=lambda: check_state(Button1))
MyCheckbutton2.config(command=lambda: check_state(Button2))