如何使用新定义的变量更新tkinter窗口?

时间:2019-04-24 16:37:22

标签: python python-3.x tkinter

我想创建两个窗口,其中第一个是一个按钮,第二个是一个标签。 当我按下按钮时,第二个窗口中的标签应刷新并显示变量加1。

我不想关闭第二个窗口,然后再将其打开。 有什么想法可以做到吗?

import tkinter as tk

text = 1
root = tk.Tk()
root2 = tk.Tk()

label = tk.Label(root, bg="green", text=text, fg="blue")
label.place(relx=0.1, rely=0.1, relheight=0.3, relwidth=0.3)

def plus():
    global text
    text = text + 1

    print(text)


button = tk.Button(root2, bg="blue", text="Button", command=plus)
button.pack()

root.mainloop()
root2.mainloop()

此代码应该具有基础知识,但仍然缺少刷新第二个窗口的行。

希望任何人都可以告诉我该如何做:)

4 个答案:

答案 0 :(得分:0)

您不能在一个线程中运行两个主循环。您对root.mainloop()的调用将一直阻塞,直到您关闭窗口。关闭窗口后,它会退出该方法并运行root2.mainloop(),因为它是下一行代码。

一个根窗格可以具有多个窗口,您无需创建root2。您要制作两个Toplevel windows

答案 1 :(得分:0)

要扩展已经给出的答案,您应该使用“顶级”而不是两个根窗口。 同样,为了更改标签的文本,您可以使用<div class="band"> <div class="contain"> <div class="row"> <div class="col"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> <div class="col col-image"> <img src="https://via.placeholder.com/800x450?text=fpo"> </div> </div> </div> </div>来更新值。

label['text'] = str(text)

答案 2 :(得分:0)

如果我了解的话,请尝试此操作,每次打开顶层窗口时,新窗口中的标签都会增加1

 { "dp": "http://localhost:4000/file/5cc0905839355d013c040794"}

答案 3 :(得分:0)

使用Toplevel()窗口而不是将Tk()用作辅助窗口。 Tkinter是单线程的,因此对所有窗口使用一个mainloop()就足够了。

现在,您要在一个窗口中想要一个按钮,并在第二个窗口中更新一个标签,并且如果您关闭第二个窗口,则可以重新打开该窗口而不会丢失任何进度。

这是我的方式。

import tkinter as tk

text = 1
root = tk.Tk()
root2 = tk.Toplevel()

label = tk.Label(root2, bg="green", text=text, fg="blue")
label.place(relx=0.1, rely=0.1, relheight=0.3, relwidth=0.3)

def plus():
    global text, root2, label
    if not root2.winfo_exists():  # checks if the window is closed or not
        root2 = tk.Toplevel()
        label = tk.Label(root2, bg="green", text=text, fg="blue")
        label.place(relx=0.1, rely=0.1, relheight=0.3, relwidth=0.3)

    text += 1
    label['text'] = text

button = tk.Button(root, bg="blue", text="Button", command=plus)
button.pack()

root.mainloop()

使用IntVar()更新所有标签值。可以将StringVar()用作字符串类型:

样品

import tkinter as tk

root = tk.Tk()
root2 = tk.Toplevel()

# This is a textvariable IntVar can also use StringVar for sting input
text = tk.IntVar()   
text.set(1)             # Set the initial to 1

# Use 'textvariable' instad of text
label = tk.Label(root2, bg="green", textvariable=text, fg="blue")
label.place(relx=0.1, rely=0.1, relheight=0.3, relwidth=0.3)

label2 = tk.Label(root, textvariable=text)
label2.pack()

def plus():
    global text, root2, label
    if not root2.winfo_exists():  # checks if the window is closed or not
        root2 = tk.Toplevel()
        label = tk.Label(root2, bg="green", textvariable=text, fg="blue")
        label.place(relx=0.1, rely=0.1, relheight=0.3, relwidth=0.3)

    text.set( text.get()+1 )     # incrementing by 1 ( text.get() will return the value )

button = tk.Button(root, bg="blue", text="Button", command=plus)
button.pack()

root.mainloop()

我希望您可以从我的答案中找到有用的东西。