Python(Tkinter):我一辈子都不能让我的小部件出现在风中

2024-04-28 21:51:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在用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()

Tags: selfgetifishavecolumntkgrid
2条回答

阅读有关创建MCVE的内容。我这样做了,用较少的分心线,发现了多个错误:

import tkinter as tk
root = tk.Tk()

class application(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.grid()

        self.stars = tk.StringVar(parent, 'something')
        self.textBox = tk.Label(self, textvariable= self.stars)
        self.textBox.grid(column=1, row=0)

app = application(root)
root.title('1.3 TKinter implementation')
root.mainloop()

__init__有两个双下划线。`StringVars需要与其他所有内容具有相同的父级。依赖默认根会导致麻烦。应用程序应该明确地创建一个根目录,并将其用于直接放入根目录的所有字体、变量、顶层和小部件。然后一切如期进行。你知道吗

我花了一段时间,休息了一会儿,但我终于把程序修好了。原来CheckButton不是声明为CheckButton,而是声明为Checkbutton-\

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()

相关问题 更多 >