【发布时间】:2019-02-17 04:34:17
【问题描述】:
我正在尝试运行一个 while 循环来更改我的代码中的变量,但它只会破坏我的代码。我不知道该怎么办。当我改变 T 而不重复命令时,我希望 X 改变。
import tkinter
from tkinter import *
code = Tk()
T = 1
X = 0
def printx():
global X
print(X);
def teez():
global T
T = 0;
def teeo():
global T
T = 1;
while T == 1:
X = 5
else:
X = 6
button1 = Button(code, text = "Print X", command = printx)
button1.pack()
button2 = Button(code, text = "T = 0", command = teez)
button2.pack()
button2 = Button(code, text = "T = 1", command = teeo)
button2.pack()
code.mainloop()
附:它在 python 3.7 中
【问题讨论】:
-
它破坏你的代码的原因是它是一个无限循环。我不确定你想做什么。
-
你还需要在你的while循环中有一个
if...else。也插入一个 break 语句 -
@khelwood 我希望它在我更改 T 时更改 x 但我不希望它在函数内部。
-
我会后退一步,问你为什么不想在函数内部更改 X?编写的 while 循环永远不会让您的代码到达 tkinter 按钮定义。
-
我认为您正在尝试执行 if 语句。如果您能告诉我们您想通过 while 循环实现什么目标,我们可以帮助您解决问题
标签: python python-3.x variables tkinter while-loop