简体   繁体   English

GUI 程序无法运行 PYTHON

[英]GUI program not working PYTHON

I was trying to make a simple GUI based game that has a button having the text CLICK ME .Whenever the user clicks on the button the total number clicks are displayed on the button.我试图制作一个简单的基于 GUI 的游戏,它有一个带有文本 CLICK ME 的按钮。每当用户点击按钮时,总点击次数就会显示在按钮上。

Here is my code这是我的代码

from Tkinter import *

    class Application(Frame):

        def __init__(self,master):
            Frame.__init__(self,master)
            self.grid()
            self.bttn_click = 0
            self.create_widget()

        def create_widget(self):
            self.bttn = Button(self)
            self.bttn["text"] = "Total Clicks = 0"
            self.bttn["command"] = self.update_count()
            self.bttn.grid()

        def update_count(self):
            self.bttn_click += 1
            self.bttn["text"] = "Total Clicks = " + str(self.bttn_click)

    #main

    root = Tk()
    root.geometry("900x700")
    root.title("Click Counter")

    app = Application(root)

    root.mainloop()

Please read it from the official documentation .请从官方文档中阅读。 In there, the first "Hello World" Example has almost the same code as you do.在那里,第一个“Hello World”示例的代码与您几乎相同。

The command self.bttn["command"] = self.update_count() assigns the return value of def update_count(self) to your button command.命令self.bttn["command"] = self.update_count()def update_count(self)的返回值分配给您的按钮命令。

If you think to know what the result of some action should be, you could always use a print statement afterwards to verify what your assignment did.如果你想知道某个动作的结果应该是什么,你可以在之后使用打印语句来验证你的任务做了什么。

self.bttn["command"] = self.update_count()
print(self.bttn["command"])

What exactly is the issue and where does it come from?究竟是什么问题,它来自哪里?

in the code line of yours mentioned above, you are immediately calling self.update_count and not assigning the function to be called everytime the button is pressed.在上面提到的代码行中,您立即调用self.update_count并且没有分配每次按下按钮时要调用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM