尝试在 Tkinter 中使用类来创建多个框架
Trying to use classes in Tkinter to create multiple frames
我正在尝试在我的主要父窗口小部件中创建 2 个框架 - 一个使用 grid()
方法,另一个使用 pack()
方法。我首先以程序的方式编写它,它太混乱了,所以我试图使用类来清理代码。下面是相关代码,我尝试使用 init 方法创建 2 个帧,但它会抛出各种错误。
from tkinter import *
class Application(Frame):
"""This class creates packed frames for the GUI"""
def __init__(self, master):
Frame.__init__(self, master)
self.framepack = Frame(master)
self.framepack.pack(side=BOTTOM, fill=X)
self.framegrid = Frame(master)
self.framegrid.pack(side=TOP)
self.create_widgets()
def create_widgets(self):
#Container 1 using LabelFrame, houses email and password labels and entries
self.inputlabels1 = LabelFrame(self, self.framepack, text="Input email login information here")
self.inputlabels1.grid(row=0, column=0, padx=10, pady=10)
self.emailfield = Label(self.inputlabels1, text="Email Address") #Labels
self.passfield = Label(self.inputlabels1, text="Password")
root = Tk()
app = Application(root)
root.mainloop()
整件事都分崩离析了。问题代码是这样的:
self.inputlabels1 = LabelFrame(self, self.framepack, text="Input email login information here")
我正在尝试将此 Labelframe 小部件放入我之前初始化的两个框架之一 framepack 中,但它会引发错误。我的语法有问题吗?
您正在尝试给 LabelFrame
两个 parent。 parent 应该是 self.framepack
。删除第一个参数应该可以解决问题:
self.inputlabels1 = LabelFrame(self.framepack, text="Input email login information here")
我修改了你的例子,让它工作,并显示一些东西。在目前的形式下,它不起作用。另外,我给框架添加了颜色,这样它们就很容易被看到。这有助于组织您的布局,并了解正在发生的事情:
from tkinter import *
class Application(Frame):
"""This class creates packed frames for the GUI"""
def __init__(self, master):
Frame.__init__(self, master)
# added width=180, height=40, background='red'
self.framepack = Frame(master, width=180, height=40, background='red')
self.framepack.pack(side=BOTTOM, fill=X)
# added width=180, height=40, background='green'
self.framegrid = Frame(master, width=180, height=40, background='green')
self.framegrid.pack(side=TOP)
self.create_widgets()
def create_widgets(self):
# Container 1 using LabelFrame, houses email and password labels and entries
# Removed `self,` from the orginal code.
self.inputlabels1 = LabelFrame(self.framepack, text="Input email login information here")
self.inputlabels1.grid(row=0, column=0, padx=10, pady=10)
self.emailfield = Label(self.inputlabels1, text="Email Address") #Labels
# Called pack()
self.emailfield.pack()
self.passfield = Label(self.inputlabels1, text="Password")
# Called pack()
self.passfield.pack()
root = Tk()
app = Application(root)
root.mainloop()
希望对您有所帮助。
我正在尝试在我的主要父窗口小部件中创建 2 个框架 - 一个使用 grid()
方法,另一个使用 pack()
方法。我首先以程序的方式编写它,它太混乱了,所以我试图使用类来清理代码。下面是相关代码,我尝试使用 init 方法创建 2 个帧,但它会抛出各种错误。
from tkinter import *
class Application(Frame):
"""This class creates packed frames for the GUI"""
def __init__(self, master):
Frame.__init__(self, master)
self.framepack = Frame(master)
self.framepack.pack(side=BOTTOM, fill=X)
self.framegrid = Frame(master)
self.framegrid.pack(side=TOP)
self.create_widgets()
def create_widgets(self):
#Container 1 using LabelFrame, houses email and password labels and entries
self.inputlabels1 = LabelFrame(self, self.framepack, text="Input email login information here")
self.inputlabels1.grid(row=0, column=0, padx=10, pady=10)
self.emailfield = Label(self.inputlabels1, text="Email Address") #Labels
self.passfield = Label(self.inputlabels1, text="Password")
root = Tk()
app = Application(root)
root.mainloop()
整件事都分崩离析了。问题代码是这样的:
self.inputlabels1 = LabelFrame(self, self.framepack, text="Input email login information here")
我正在尝试将此 Labelframe 小部件放入我之前初始化的两个框架之一 framepack 中,但它会引发错误。我的语法有问题吗?
您正在尝试给 LabelFrame
两个 parent。 parent 应该是 self.framepack
。删除第一个参数应该可以解决问题:
self.inputlabels1 = LabelFrame(self.framepack, text="Input email login information here")
我修改了你的例子,让它工作,并显示一些东西。在目前的形式下,它不起作用。另外,我给框架添加了颜色,这样它们就很容易被看到。这有助于组织您的布局,并了解正在发生的事情:
from tkinter import *
class Application(Frame):
"""This class creates packed frames for the GUI"""
def __init__(self, master):
Frame.__init__(self, master)
# added width=180, height=40, background='red'
self.framepack = Frame(master, width=180, height=40, background='red')
self.framepack.pack(side=BOTTOM, fill=X)
# added width=180, height=40, background='green'
self.framegrid = Frame(master, width=180, height=40, background='green')
self.framegrid.pack(side=TOP)
self.create_widgets()
def create_widgets(self):
# Container 1 using LabelFrame, houses email and password labels and entries
# Removed `self,` from the orginal code.
self.inputlabels1 = LabelFrame(self.framepack, text="Input email login information here")
self.inputlabels1.grid(row=0, column=0, padx=10, pady=10)
self.emailfield = Label(self.inputlabels1, text="Email Address") #Labels
# Called pack()
self.emailfield.pack()
self.passfield = Label(self.inputlabels1, text="Password")
# Called pack()
self.passfield.pack()
root = Tk()
app = Application(root)
root.mainloop()
希望对您有所帮助。