我正在自学 Python 并尝试构建一个使用窗口 GUI 的本地应用程序。我在尝试使用grid()
. 我已经搜索并尝试了许多不同的代码片段,但我遇到了同样的问题,框架和小部件似乎没有格式化。下面的代码非常简单,但我的最终目标是掌握如何使用grid()
,以便将来构建我喜欢的任何 GUI。
我希望能够做到以下几点:
--Window-----------------
| Section 1 | Section 2 |
| | |
-------------------------
| Section 3 |
| |
| |
-------------------------
from Tkinter import Button, Frame, Entry, Tk, Label, Menubutton, Menu, IntVar
class MainScreen(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.searchSection()
self.quitButton()
def searchSection(self):
# Create Search Section
self.searchFrame = Frame(self.master, bg='grey', relief='sunken', width=200, height=200)
self.searchFrame.grid(row=0, column=0, rowspan=5, columnspan=30, sticky="wens")
Label(self.searchFrame, text="Search :", bg='grey').grid(row=1, column=1, columnspan=20, sticky='w')
self.searchField = Entry(self.searchFrame)
self.searchField.grid(row=2, column=1, columnspan=7, sticky='w')
#Create Menu Options
self.search = Menubutton(self.searchFrame, text = "Search", bg='grey')
self.search.grid(row=2, column=8, columnspan=3, sticky='w')
self.search.menu = Menu(self.search, tearoff = 0)
self.search['menu'] = self.search.menu
self.SearchType1Var = IntVar()
self.search.menu.add_checkbutton(label="SearchType1", variable = self.SearchType1Var)
def quitButton(self):
## Provide a quit button to exit the rogram
self.quitFrame = Frame(self.master, bg='grey', width=50, height=50)
self.quitFrame.grid(row=0, column=20, rowspan=5, columnspan=5, sticky='ewns')
self.quitButton = Button(self.quitFrame, text="Quit", command=exit)
self.quitButton.grid()
if __name__ == '__main__':
root = Tk()
root.title("Learn Grid GUI")
root.geometry("800x600+200+150")
main = MainScreen(root)
root.mainloop()