简体   繁体   English

如何使用 Tkinter 从不同的函数重新分配标签文本

[英]How do I reassign Label text with Tkinter from a different function

from tkinter import *




# reassigns variables for label and button texts
def Ctrack(vname, vvalue):
    #global vname
    vname = vvalue
    
    #place holder to check which button has been clicked
    print(vname)
    
    #labeltext = reassign label text bas
    
    
# creates labels and buttons
def buttons():
    root = Tk()
    
    #first label
    
    winlabel = Label(root, text =labeltext )
    winlabel.grid(row = 0, column = 1, padx = 10, pady = 10)
    
    #first button
    but1 = Button(root, text = "button 1", command = lambda *args: Ctrack(0, 1))
    but1.grid(row = 0, column = 2, padx = 10, pady = 10)
    
    #second button
    but2 = Button(root, text = "button 2", command = lambda *args: Ctrack(0, 2))
    but2.grid(row = 0, column = 3, padx = 10, pady = 10)
    
    root.mainloop()

buttons()

(first time using Stackoverflow) (第一次使用 Stackoverflow)

How do I reassign the label text in my buttons function to whatever I assign the variable in the Ctrack function?如何将按钮函数中的标签文本重新分配给我在 Ctrack 函数中分配的变量?

You can give the Ctrack() function the ability to post the Label on the root window.您可以让 Ctrack() 函数能够在根窗口上发布标签。 If you go with that approach, you can add 'root' as an argument in Ctrack() and then declare-and-pack the Label within the Ctrack() function.如果您采用这种方法,您可以在 Ctrack() 中添加“root”作为参数,然后在 Ctrack() 函数中声明并打包标签。

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

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