我一直在用python开发一个小型GUI应用程序,我就是不能让小部件出现在屏幕上,有人能指出我的错误吗?程序运行,但是,只有一个空窗口出现,没有小部件。你知道吗
import tkinter as tk
class application(tk.Frame):
def _init_(self, parent):
tk.Frame._init_(self)
self.createWidgets()
self.generateText()
def createWidgets(self):
self.grid()
self.stars = tk.StringVar()
self.textBox = tk.Label(self, textvariable= self.stars)
self.firstVar = tk.StringVar()
self.checkBox1 = tk.CheckButton(self, text='Have you finished sweeping and mopping?',
variable=self.firstVar, onvalue='yes', offvalue='no')
self.secondVar = tk.StringVar()
self.checkBox2 = tk.CheckButton(self, text='Have you finished preparing food?',
variable=self.secondVar, onvalue='yes', offvalue='no')
self.thirdVar = tk.StringVar()
self.checkBox3 = tk.CheckButton(self, text='Have you finished mowing the lawn?',
variable=self.thirdVar, onvalue='yes', offvalue='no')
self.textBox.grid(column=1, row=0)
self.checkBox1.grid(column=1, row=1)
self.checkBox2.grid(column=1, row=2)
self.checkBox3.grid(column=1, row=3)
def generateText(self):
if self.firstVar.get() is 'yes':
self.stars.set('Here, have a gold star')
if self.secondVar.get() is 'yes':
self.stars.set('Nice!, have a gold star')
if self.thirdVar.get() is 'yes':
self.stars.set('Good job, have a gold star')
if (self.firstVar.get() is 'yes' and self.secondVar.get() is 'yes' and self.thirdVar.get() is 'yes'):
self.stars = 'You have 3 gold stars, what a good boy.'
app = application(None)
app.master.title('1.3 TKinter implementation')
app.mainloop()
阅读有关创建MCVE的内容。我这样做了,用较少的分心线,发现了多个错误:
__init__
有两个双下划线。`StringVars需要与其他所有内容具有相同的父级。依赖默认根会导致麻烦。应用程序应该明确地创建一个根目录,并将其用于直接放入根目录的所有字体、变量、顶层和小部件。然后一切如期进行。你知道吗我花了一段时间,休息了一会儿,但我终于把程序修好了。原来CheckButton不是声明为
CheckButton
,而是声明为Checkbutton
-\相关问题 更多 >
编程相关推荐