Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python tkinter中更新标签变量_Python_Variables_Tkinter_Random_Label - Fatal编程技术网

在python tkinter中更新标签变量

在python tkinter中更新标签变量,python,variables,tkinter,random,label,Python,Variables,Tkinter,Random,Label,这段代码运行良好,但当我按下按钮时,它不会给我一个随机数。我需要它给我一个随机输出,这样我就可以从列表中得到一个随机元素。有人知道这个问题的答案吗?在这里。现在,您可以看到标签发生了更改: 从tkinter导入* 将tkinter作为tk导入 随机输入 def测试_打印(): f=random.randint(01118) label.configure(text=str(f)) master.update_idletasks() #创建Tk窗口 master=Tk() 硕士学位(600400)

这段代码运行良好,但当我按下按钮时,它不会给我一个随机数。我需要它给我一个随机输出,这样我就可以从列表中得到一个随机元素。有人知道这个问题的答案吗?

在这里。现在,您可以看到标签发生了更改:

从tkinter导入*
将tkinter作为tk导入
随机输入
def测试_打印():
f=random.randint(01118)
label.configure(text=str(f))
master.update_idletasks()
#创建Tk窗口
master=Tk()
硕士学位(600400)
#获得一个可根据需要扩展的Fra,e
#和窗户一样大
窗格=框架(主)
pack(fill=BOTH,expand=True)
#按钮小部件,也可以扩展和填充
#在父窗口小部件中
#按钮1
b1=按钮(窗格,text=“单击我!”,
背景=“红色”,fg=“白色”,命令=测试(打印)
b1.包装(侧面=顶部,展开=正确,填充=两者)
label=tk.label(窗格)
label.pack(侧面=底部,展开=假)
#执行Tkinter
master.mainloop()
答案如下

from tkinter import *
import tkinter as tk
import random

f = 0

def test_print():
    f = random.randint(0,118)
    master.update_idletasks()

# creating Tk window
master = Tk()

master.minsize(600,400)
 
var = IntVar()
var.set(f)

# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
 
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !", 
            background = "red", fg = "white", command = test_print)
b1.pack(side = TOP, expand = True, fill = BOTH)
 
label = tk.Label(pane, textvariable = var)
label.pack(side = BOTTOM, expand = False)

# Execute Tkinter
master.mainloop()

您只需在函数内部说
var.set('whatever')
from tkinter import *
import tkinter as tk
import random

maxnumber = 118
f = random.randint(0,maxnumber)
def test_print():
   master.update_idletasks()
   label.configure(textvariable = var)

# creating Tk window
master = Tk()

master.minsize(600,400)

var = IntVar()
var.set(f)

# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)

# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !", 
        background = "red", fg = "white", command = test_print)
b1.pack(side = TOP, expand = True, fill = BOTH)

label = tk.Label(pane)
label.pack(side = BOTTOM, expand = False)

# Execute Tkinter
master.mainloop()