如何将复选框与文件打开对话框链接并使用python tkinter显示在GUI中打开的文件的详细信息?

问题描述 投票:0回答:1

我想使用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
python tkinter
1个回答
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()

并且为了显示信息,您可以添加一个框架并在框架上显示所需的信息。

© www.soinside.com 2019 - 2024. All rights reserved.