简体   繁体   中英

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

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. To frame be placed in left side, set side to 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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