首页 > 解决方案 > How to make the GUI using Tkinter of the format specified below?

问题描述

In the code given below, I want to create a GUI in such a way that 1) In the top frame: 1st row : "x"(checkbutton), right to "x"(checkbutton) must be "Choose xFile"(button) and right to "Choose xFile"(button) must be "clear"(button) and likewise in the 2nd row : for "y". Only when the "x" checkbutton is checked, the "choose xFile" button should gets enabled. And when clicked upon "choose xFile", it has to open the file dialogbox. And the choosen file contents should get displayed using "description box" below the "Input data"(label) in the middle frame(with both horizontal and vertical scrollbar). And when "clear" button is clicked, only the selected file contents(x or y file choosen) in the "description box" must be cleared and it must enable the "Choose xFile"(button) or "Choose yFile"(button) to perform the task again(i.e to open the file dialog box). Below to the "description box" must contain "Reset" button and to the right of "Reset" button must be "Submit" button in the middle portion of the middle frame. When the "Reset" button is clicked, all the contents being displayed in the "description box" must be cleared and the all the checkboxes must be unchecked so that the user can perform the selection process again. In the bottom frame, below "Model Output"(label), a "description box" along with "horizontal" and "vertical" scrollbar must be there. Below to the "description box" must contain "Exit" button which is placed in the middle of the "bottom frame".

from tkinter import *

def forButton1():
    filename1 = askopenfilename()

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

    print(filename1)

def forButton2():
    filename1 = 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")

topFrame = LabelFrame(root, text = "Select input file")

MyVar1 = IntVar()
MyVar2 = IntVar()

MyCheckbutton1 = Checkbutton(topFrame, text="x", variable=MyVar1)
#MyCheckbutton1.grid(row=0, column=0)
MyCheckbutton1.pack()

MyCheckbutton2 = Checkbutton(topFrame, text="y", variable=MyVar2)
#MyCheckbutton2.grid(row=1, column=0)
MyCheckbutton2.pack()

Button1 = Button(topFrame, text = "Choose xFile", command = forButton1)
#button1.grid(row=0, column=1)
Button1.pack()
Button2 = Button(topFrame, text = "Choose yFile", command = forButton2)
#button2.grid(row=0, column=1)
Button2.pack()
Button3 = Button(topFrame, text = "Clear")
Button3.pack()
Button4 = Button(topFrame, text = "Clear")
Button4.pack()
topFrame.pack(side=TOP)

middleFrame = Frame(root)
label1 = Label(middleFrame, text = "Input data:")
label1.grid(row = 4)


scrollbar = Scrollbar(middleFrame)


myList = Listbox(middleFrame, yscrollcommand = scrollbar.set)
myList.pack()
scrollbar.config( command = myList.yview )
scrollbar.pack()

Button5 = Button(middleFrame, text = "Reset")
#button1.grid(row=0, column=1)
Button5.pack()

Button6 = Button(middleFrame, text = "Submit")
#button1.grid(row=0, column=1)
Button6.pack()

middleFrame.pack()

bottomFrame = Frame(root)

label2 = Label(bottomFrame, text = "Model Output:")
label2.grid(row = 10)

Button7 = Button(bottomFrame, text = "Exit", command = forButton7)
#button1.grid(row=0, column=1)
Button7.pack()

bottomFrame.pack()

root.geometry("500x500")
root.mainloop()

标签: pythonpython-3.xtkinter

解决方案


在这里,我已经修复了所询问的给定小部件(而不是它们的功能)的位置。您可以按照这种方式获得所需的格式:

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)

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

root.geometry("250x425")
root.mainloop()

推荐阅读