如何更新Tkinter消息

时间:2019-02-14 12:40:23

标签: python tkinter

我希望tk.message中有一个名为“ spe”的字符串变量,我希望在执行Identify后对其进行更改

Add after changing of `spe_win`:
spe.set(spe_win)

我认为您可以丢弃spe_win变量,而改用spe.set("Updated ...")

讨论后在下面的评论中进行编辑:

import tkinter as tk
from tkinter import messagebox
global spe="Number"
class IdPage(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(self,parent)
        tk.Button.__init__(self,parent)
        tk.Message.__init__(self,parent)
        tk.Label.__init__(self,parent)
        speak = tk.StringVar(self)
        record_identify = tk.Button(self,
            text = "Record/Identifies",
            font = ("Arial",15),
            command = self.Identify)
        speak = tk.Message(self,
            textvariable = spe,
            width=250)
        spe.set(spe)
        speak.pack()
        record_identify.pack()

global spe="Number"
def Identify():
        print("Your favourite number:")
        a=input()
        if a>5:
            spe="Updated positive"
        else:
            spe="Updated Negative"

root = tk.Tk()
frame = IdPage(root)
frame.pack()
root.mainloop()

1 个答案:

答案 0 :(得分:1)

更改spe_win后添加:

spe.set(spe_win)

我认为您可以丢弃spe_win变量,而改用spe.set("Updated ...")

讨论后在下面的评论中进行编辑:

import tkinter as tk
from tkinter import messagebox

class IdPage(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(self,parent)
        tk.Button.__init__(self,parent)
        tk.Message.__init__(self,parent)
        tk.Label.__init__(self,parent)
        self.spe = tk.StringVar(self)
        record_identify = tk.Button(self,
            text = "Record/Identifies",
            font = ("Arial",15),
            command = lambda: Identify(self.spe))
        speak = tk.Message(self,
            textvariable = self.spe,
            width=250)
        self.spe.set("How're you?")
        speak.pack()
        record_identify.pack()

def Identify(spe):
    a=4
    if a>3:
        spe.set("Updated positive")
    else:
        spe.set("Updated Negative")

root = tk.Tk()
frame = IdPage(root)
frame.pack()
root.mainloop()