为什么 tkinter 没有显示任何内容?

huangapple go评论95阅读模式
英文:

Why is tkinter not displaying anything?

问题

我正在尝试使用tkinter制作一个UI游戏但在打开的窗口中没有显示任何内容在shell中也没有错误

```python
import tkinter as tk

count = 0

def on_button_click():
    global count
    count += 1
    label.config(text="You have clicked " + str(count) + " times!")

root = tk.Tk()
root.title("My Game")
root.geometry("800x600")

# button
button = tk.Button(root, text="Click me", command=on_button_click)
button.pack()

# label
label = tk.Label(root, text="Jitter click the button")
label.pack()

root.mainloop()

脚本的输出

我尝试过搜索不同的tkinter教程,但它们都表现得一样,是否有明显的遗漏。


<details>
<summary>英文:</summary>

Im trying out tkinter to make a UI game but it  is not displaying anything in the window that opens, there is no error in the shell.


import tkinter as tk

count = 0

def on_button_click():
global count
count += 1
label.config(text="You have clicked " + str(clicks) + " times!")

root = tk.Tk()
root.title("My Game")
root.geometry("800x600")

root.mainloop()

button

button = tk.Button(root, text ="Click me", command=on_button_click)
button.pack()

label

label = tk.Label(root, text="Jitter click the button")
label.pack()

root.mainloop()

[The output of the script ](https://i.stack.imgur.com/8PFd7.png)

i have tried searching up different tutorials on tkinter but they all act the same, is it something obvious I&#39;m missing

</details>


# 答案1
**得分**: 3

你的标签和按钮创建代码位于root.mainloop()之外,并且你有两次mainloop()行。

<details>
<summary>英文:</summary>

Your label &amp; button creation code is outside your root.mainloop() and you have the mainloop() line twice. 

</details>



huangapple
  • 本文由 发表于 2023年6月26日 03:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定