这是我的代码创建注册屏幕的一个段,在运行时它不会创建任何错误,但是在GUI上没有任何标签或条目可见,只有两个按钮在tkinter窗口中可见。我使用的另一个框架使用完全相同的方法输入数据,并且工作正常。非常感谢任何帮助,谢谢!
class register(Frame):
def __init__(self,master):
super().__init__(master)
self.labelEmail = Label(self, text="Enter email:")
self.labelUsername = Label(self,text="Enter username:")
self.labelPassword = Label(self, text ="Enter password:")
self.labelPassword2 = Label(self, text ="Re-enter password:")
self.entryEmail = Entry(self)
self.entryUsername = Entry(self)
self.entryPassword = Entry(self, show="*")
self.entryPassword2 = Entry(self, show="*")
self.labelEmail.grid(row=0, sticky=E)
self.labelUsername.grid(row=1, sticky=E)
self.labelPassword.grid(row=2, sticky=E)
self.labelPassword2.grid(row=3, sticky=E)
self.entryEmail.grid(row=0, column=1)
self.entryUsername.grid(row=1, column=1)
self.entryPassword.grid(row=2, column=1)
self.entryPassword2.grid(row=3, column=1)
self.confirmBtn = Button(text="Confirm", command=self.confirmBtnClicked)
self.confirmBtn.grid(row=4, column=0)
self.cancelBtn = Button(text="Cancel",command=self.cancelBtnClicked)
self.cancelBtn.grid(row=4, column=1)
.....
root = Tk()
ls = register(root)
root.mainloop()
答案 0 :(得分:1)
因为您的类继承自Frame
,并且您要将小部件分配给self
(The Frame),您还需要将Frame分配给根窗口。
要执行此操作,您需要添加以下行:
self.grid(row=0, column=0)
或者:
self.pack(fill="both", expand=True)
# If you select this option you will need to use pack()
# for anything else you assign to the root window.
正如Bryan在评论中指出的那样,更好的解决方案是在创建类实例时调用grid()
或pack()
:
ls = register(root)
ls.pack(fill="both", expand=True)