<p>我花了一段时间,休息了一会儿,但我终于把程序修好了。原来CheckButton不是声明为<code>CheckButton</code>,而是声明为<code>Checkbutton</code>-\</p>
<pre><code>import tkinter as tk
root = tk.Tk()
class application(tk.Frame):
def genMessage(self):
if self.firstVar.get() == 1:
self.stars.set('You have 1 gold star')
if self.secondVar.get() == 1:
self.stars.set('Nice!, you have 1 gold star')
if self.thirdVar.get() == 1:
self.stars.set('Good job!, you have 1 gold star')
if (self.firstVar.get() == 1 and self.secondVar.get() == 1) or (self.firstVar.get() == 1 and self.thirdVar.get() == 1) or (self.secondVar.get() == 1 and self.thirdVar.get() == 1):
self.stars.set('Wow! You have 2 stars already!')
if self.firstVar.get() == 1 and self.secondVar.get() == 1 and self.thirdVar.get() == 1:
self.stars.set('You have 3 gold stars, what a good boy.')
def createWidgets(self):
self.starCount = 0
self.stars = tk.StringVar(self, 'No stars yet')
self.textBox = tk.Label(self, textvariable= self.stars)
self.firstVar = tk.BooleanVar(self)
self.checkBox1 = tk.Checkbutton(self, text='Have you finished sweeping and mopping?', variable=self.firstVar, onvalue = 1, offvalue = 0)
self.secondVar = tk.BooleanVar(self)
self.checkBox2 = tk.Checkbutton(self, text='Have you finished preparing food?', variable=self.secondVar, onvalue = 1, offvalue = 0)
self.thirdVar = tk.BooleanVar(self)
self.checkBox3 = tk.Checkbutton(self, text='Have you finished mowing the lawn?', variable=self.thirdVar, onvalue = 1, offvalue = 0)
self.calcBtn = tk.Button(text= 'stars?', command = self.genMessage)
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)
self.calcBtn.grid(column=1, row=4)
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.grid()
self.createWidgets()
app = application(root)
root.mainloop()
</code></pre>