我刚刚开始学习python和tkinter,我无法在窗口中显示任何小部件。它有我给它的名字,当我运行程序时窗口本身出现,但没有小部件出现,背景颜色也没有。
import Tkinter
class mainthing(Tkinter.Tk):
def _init_(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid(bg="#001a00")
self.topEntry = Tkinter.Entry(self, bg = "#006600", fg = "#00ff00")
self.topEntry.grid(column=0, row=1, sticky="EW")
topEntry.pack()
yesBut = Tkinter.Button(self, text="Yes")
yesBut.grid(column=1, row=1)
query = Tkinter.Label(self, fg="#00ff00", bg="#001a00", anchor="W")
query.grid(column=1, row=0, columnspan=2, sticky="EW")
if __name__ == "__main__":
app = mainthing(None)
app.title("RUN ON START TEST")
app.mainloop()
任何帮助将不胜感激。
答案 0 :(得分:2)
我刚刚开始学习Python和tkinter今天早些时候
我认为现在发布问题为时尚早,因为您需要阅读有关Python和Tkinter的更多信息。您的问题可能会被关闭,但我会给您一个简短描述错误的解决方案:
__init__()
而不是_init()_
Frame
Tk
而不是class mainthing(Tkinter.Tk)
Tkinter.Tk.__init__(self, parent)
更改为Tkinter.Frame.__init__(self, parent)
self.grid(bg="#001a00")
。删除它。Canvas
或Frame
。pack()
和grid()
。以下是使用Frame
的快速解决方案:
import Tkinter as Tk
class mainthing(Tk.Frame):
def __init__(self,parent):
Tk.Frame.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.parent.title("RUN ON START TEST")
self.parent.grid_rowconfigure(0,weight=1)
self.parent.grid_columnconfigure(0,weight=1)
self.parent.config(background="red")
self.frame = Tk.Frame(self.parent)
self.frame.pack(fill=Tk.X, padx=5, pady=5)
self.topEntry = Tk.Entry(self.frame, bg = "#006600", fg = "#00ff00")
self.topEntry.grid(column=0, row=1, sticky="ew")
yesBut = Tk.Button(self.frame, text="Yes")
yesBut.grid(column=1, row=1)
query = Tk.Label(self.frame, fg="#00ff00", bg="#001a00", anchor="w")
query.grid(column=1, row=0, columnspan=2, sticky="ew")
if __name__ == "__main__":
root=Tk.Tk()
app = mainthing(root)
root.mainloop()
答案 1 :(得分:2)
有一些较小的错误:
__init__
,带有两个下划线topEntry.pack
是不必要的,因为您使用grid
通过这些更正,代码如下所示:
import Tkinter
class mainthing(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.config(bg="#001a00")
self.topEntry = Tkinter.Entry(self, bg = "#006600", fg = "#00ff00")
self.topEntry.grid(column=0, row=1, sticky="EW")
yesBut = Tkinter.Button(self, text="Yes")
yesBut.grid(column=1, row=1)
query = Tkinter.Label(self, fg="#00ff00", bg="#001a00", anchor="w")
query.grid(column=1, row=0, columnspan=2, sticky="EW")
if __name__ == "__main__":
app = mainthing(None)
app.title("RUN ON START TEST")
app.mainloop()