我想使用python tkinter代码创建一个GUI,其中必须包含:复选框,文件打开对话框,描述/详细信息框和提交按钮
仅在GUI中选中文件打开对话框时,才打开文件打开对话框。
例如,有2个复选框 1.土壤2.天气
仅当选中“土壤”复选框时,才应打开文件打开框,并且它必须在控制台中打印打开的文件的路径,并对“天气”重复相同的操作。
所选文件的详细信息应显示在界面的右侧。
最后,需要包含一个提交按钮。单击提交按钮后,界面应关闭。
from tkinter import * #imports
from tkinter import Tk
from tkinter.filedialog import askopenfilename
win = Tk() #create instance
win.title("Spatialization of DSSAT model")
w = 160 # width for the Tk root
h = 100 # height for the Tk root
# get screen width and height
ws = win.winfo_screenwidth() # width of the screen
hs = win.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
# set the dimensions of the screen
# and where it is placed
win.geometry('%dx%d+%d+%d' % (w, h, x, y))
def var_states():
print("soil: %d, \nweather:%d" % (var1.get(), var2.get()))
Label(win, text="Select:").grid(row=0, sticky=W)
var1 = IntVar()
Checkbutton(win, text = "soil", variable=var1).grid(row=1, sticky=W)
var2 = IntVar()
Checkbutton(win, text = "weather", variable=var2).grid(row=2, sticky=W)
MyButton1 = Button(win, text="Submit", width=10)
MyButton1.grid(row=10, column=10)
Tk().withdraw()
filename1 = askopenfilename()
print(filename1)
Tk().withdraw()
filename2 = askopenfilename()
print(filename2)
win.mainloop() #start the GUI
答案 0 :(得分:0)
您可以将command=someFunction
用于检查按钮和提交按钮,然后在someFunction()
中编写单击该按钮时要完成的操作。
尝试一下:
from tkinter import *
from tkinter import Tk
from tkinter.filedialog import askopenfilename
win = Tk()
win.title("Spatialization of DSSAT model")
w = 160
h = 100
ws = win.winfo_screenwidth()
hs = win.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
win.geometry('%dx%d+%d+%d' % (w, h, x, y))
def forCheckbutton1():
filename1 = askopenfilename()
print(filename1)
def forCheckbutton2():
filename2 = askopenfilename()
print(filename2)
def forMuButton1():
win.destroy()
def var_states():
print("soil: %d, \nweather:%d" % (MyVar1.get(), MyVar2.get()))
MyLabel1 = Label(win, text="Select:")
MyLabel1.grid(row=0, column=0, sticky=W)
MyVar1 = IntVar()
MyVar2 = IntVar()
MyCheckbutton1 = Checkbutton(win, text="soil", variable=MyVar1, command=forCheckbutton1)
MyCheckbutton1.grid(row=1, column=0, sticky=W)
MyCheckbutton2 = Checkbutton(win, text="weather", variable=MyVar2, command=forCheckbutton2)
MyCheckbutton2.grid(row=2, column=0, sticky=W)
MyButton1 = Button(win, text="Submit", width=10, command=forMuButton1)
MyButton1.grid(row=3, column=0)
win.mainloop()
为显示信息,您可以添加框架并在该框架上显示所需的信息。