如何在tkinter-label中显示变量值

时间:2019-06-13 11:13:22

标签: python tkinter

这是我第一次尝试tkinter。我为应用程序使用了一种面向对象的方法,但是无法在tkinter标签中显示变量值。我正在使用python 3.7.1。

我尝试了其他帖子中的许多建议,例如将字符串转换为tkinter的StringVar()并使用'text = ...'和'textvariable = ...'进行显示,但到目前为止,一切都已证明没结果。

import tkinter as tk

class NPCApp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        self.profession = None

        for F in (StartPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, stick="nsew")

        self.show_frame(StartPage)


    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

    def setProfession(self, given_profession):
        self.profession = given_profession

    def getProfession(self):
        return self.profession


class StartPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        tk.Label(self, text="Choose a profession for your NPC:").pack(pady=10, padx=10)

        tk.Radiobutton(self, text="Herbalist/Alchemist", variable=controller.profession, value="Herbalist", command=lambda: controller.setProfession("Herbalist")).pack()
        tk.Radiobutton(self, text="Blacksmith", variable=controller.profession, value="Smith", command=lambda: controller.setProfession("Smith")).pack()
        tk.Radiobutton(self, text="Shopkeeper", variable=controller.profession, value="Shopkeep", command=lambda: controller.setProfession("Shopkeep")).pack()
        tk.Radiobutton(self, text="Innkeep", variable=controller.profession, value="Innkeep", command=lambda: controller.setProfession("Innkeep")).pack()
        tk.Radiobutton(self, text="Officer of the city watch", variable=controller.profession, value="Officer", command=lambda: controller.setProfession("Officer")).pack()
        tk.Radiobutton(self, text="Member of the city watch", variable=controller.profession, value="Soldier", command=lambda: controller.setProfession("Soldier")).pack()
        tk.Radiobutton(self, text="Member of a council of elders", variable=controller.profession, value="Councilmember", command=lambda: controller.setProfession("Councilmember")).pack()
        tk.Radiobutton(self, text="Regular citizen", variable=controller.profession, value="Citizen", command=lambda: controller.setProfession("Citizen")).pack()
        tk.Radiobutton(self, text="Foreign human", variable=controller.profession, value="Foreigner", command=lambda: controller.setProfession("Foreigner")).pack()

        tk.Button(self, text="To Page One",
                            command=lambda: controller.show_frame(PageOne)).pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        tk.Label(self, text=controller.getProfession()).pack(pady=10, padx=10)

        tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage)).pack()


app = NPCApp()
app.mainloop()

您在这里看到的是我的申请。您可以使用单选按钮从NPC专业人士中进行选择,并且在内部设置了相应的值,该值已经过测试并且可以正常工作。但是,在标签的PageOne上显示该值不起作用。该页面显示一个空白,后跟“返回首页”按钮,但应显示所选的职业,然后在其下方显示该按钮。

帮我堆栈溢出,您是我唯一的希望。

1 个答案:

答案 0 :(得分:1)

我建议您使getProfession返回一个StringVar并更改setProfession以设置该StringVar(在__init__中创建它。然后,您可以使用{{1 }},并且只要您调用textvariable=controller.getProfession(),标签文本就会从StringVar中更新。

setProfession