简体   繁体   English

如何在Python Tkinter中添加框架?

[英]How to add frames in Python tkinter?

In the code given below, I want to open the file open dialog box only when the "soil" checkbutton is checked and repeat the same for "weather" checkbutton. 在下面给出的代码中,我只想在选中“土壤”复选框时打开文件打开对话框,然后对“天气”复选框重复相同的操作。

The "soil" Checkbutton, "weather" Checkbutton & the "Submit" button needs to be placed LEFT side of the frame of the GUI & The scrollbar should contain the details of the 2 opened files(the files selected from the "file open dialog box" which in the "text" format) which is to be placed in the RIGHT side of the frame of the GUI 需要将“土壤”检查按钮,“天气”检查按钮和“提交”按钮放置在GUI框架的左侧,并且滚动条应包含2个打开文件的详细信息(从“文件打开”对话框中选择的文件框”(以“文本”格式),将其放置在GUI框架的右侧

from tkinter import *
from tkinter import Tk
from tkinter.filedialog import askopenfilename

win = Tk()

frame = Frame(win)
frame.pack()

rightframe = Frame(win)
rightframe.pack( side = RIGHT )

#frame_name = Frame(win)
#frame_address = Frame(win)

win.title("Spatialization of DSSAT model")

w = 800
h = 400

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(frame, text="Select:")
MyLabel1.grid(row=0, column=0, sticky=W)


MyVar1 = IntVar()
MyVar2 = IntVar()

MyCheckbutton1 = Checkbutton(frame, text="soil", variable=MyVar1, command=forCheckbutton1)
MyCheckbutton1.grid(row=1, column=0, sticky=W)

MyCheckbutton2 = Checkbutton(frame, text="weather", variable=MyVar2, command=forCheckbutton2)
MyCheckbutton2.grid(row=2, column=0, sticky=W)


MyButton1 = Button(frame, text="Submit", width=10, command=forMuButton1)
MyButton1.grid(row=5, columnspan=3)

scrollbar = Scrollbar(rightframe)
scrollbar.pack( side = RIGHT, fill = Y )

myList = Listbox(rightframe, yscrollcommand = scrollbar.set )
forCheckbutton1()
forCheckbutton2()

myList.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = myList.yview )

win.mainloop()

Remove forCheckbutton1() and forCheckbutton2() from mainloop to open dialog box only when the buttons checked. 仅在选中按钮时才从主forCheckbutton2()删除forCheckbutton1()forCheckbutton2()以打开对话框。 To frame be placed in left side, set side to LEFT . 要将框放置在左侧,请将side设置为LEFT And for writhing file content in scrollbar, open file in function. 为了使滚动条中的文件内容完整,请在功能中打开文件。 Try this: 尝试这个:

from tkinter import *
from tkinter import Tk
from tkinter.filedialog import askopenfilename

win = Tk()

frame = Frame(win)
frame.pack(side=LEFT)

rightframe = Frame(win)
rightframe.pack( side = RIGHT )

win.title("Spatialization of DSSAT model")

w = 800
h = 400

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()

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

    print(filename1)

def forCheckbutton2():
    filename2 = askopenfilename()

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

    print(filename2)

def forMuButton1():
    win.destroy()

def var_states():
    print("soil: %d, \nweather:%d" % (MyVar1.get(), MyVar2.get()))

MyLabel1 = Label(frame, text="Select:")
MyLabel1.grid(row=0, column=0, sticky=W)


MyVar1 = IntVar()
MyVar2 = IntVar()

MyCheckbutton1 = Checkbutton(frame, text="soil", variable=MyVar1, command=forCheckbutton1)
MyCheckbutton1.grid(row=1, column=0, sticky=W)


MyCheckbutton2 = Checkbutton(frame, text="weather", variable=MyVar2, command=forCheckbutton2)
MyCheckbutton2.grid(row=2, column=0, sticky=W)


MyButton1 = Button(frame, text="Submit", width=10, command=forMuButton1)
MyButton1.grid(row=5, columnspan=3)

scrollbar = Scrollbar(rightframe)
scrollbar.pack( side = RIGHT, fill = Y )

myList = Listbox(rightframe, yscrollcommand = scrollbar.set )
myList.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = myList.yview )

win.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM