【问题标题】:How to refresh the screen in tkinter to show a new label text?如何在 tkinter 中刷新屏幕以显示新的标签文本?
【发布时间】:2018-06-11 04:10:48
【问题描述】:

下面是我的代码,我正在为自己制作一个有趣的刽子手游戏,以适应 Tkinter 库/GUI 创建和 python 语言。在用户输入猜测以使用标签向用户提供反馈后,我在刷新 GUI 时遇到了一些障碍。我读到使用标签,所以你可以附加一个字符串变量,这样你就可以更改文本,但我无法弄清楚。无论出于何种原因,我的 Tkinter 标签“text_out”都没有显示。提前感谢您的帮助!此外,我是新手和初学者程序员,所以任何关于如何更好地组织自己的意见也会很棒。

    from tkinter import *
    import random

    root = Tk()
    root.wm_title("Hangman")

    w = Canvas(root, width=1000,height=650)
    #drawing the hangmaan image setup
    w.create_rectangle(250,0,450,25, fill = "brown") #top beam
    w.create_rectangle(250,0,275,100, fill = "brown") #hanging part
    w.create_rectangle(475,0,450,400, fill = "brown") #middle upright
    w.create_rectangle(300,400,600,425, fill = "brown") #platform
    w.pack()

    alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y",
        "z"] #keeps track of letters used
    words = ["atmosphere", "miscellaneous","muffins","junk","pajamas","engineering","privilege","amplifier","countryside","python",
     "magic","hats","flash","clock","ceiling"] #random word choices for the answer

    word_choice = random.choice(words) #chooses word from list of words for         the answer
    print(word_choice) #for debugging
    mult = len(word_choice) #length of word to create underscore string 
    underscores = "_ "*mult #underscore string 
    print(underscores) #for debugging
    w.create_text(500,600, font= ("Purisa", 48), text = underscores) #underscores in gui output

    guess_out = "$" #text varavable to be changed as a result of user guess
    text_out = Label(root,font=("Purisa", 18),textvariable="Test").place(x=650,y=200) #results of user guess text on screen

    guess_bar_label = Label(root, font = ("Purisa", 20), text = "Guess: ").place(x=600, y=285) #label for the text bar
    guess_var = StringVar() #varaiable for the text bar
    guess_bar = Entry(root, textvariable=guess_var).place(x=700, y=300) #text bar for user input


    def Guess():
        #logic test 
        print(guess_var.get())
        if len(str(guess_var.get()))==1: #makes sure input is only one charactar
            print("one char")
            if str(guess_var.get()) in alphabet: #makes sure is still a choice and not one they have picked already
                print("in alphabet") #for debugging
                guess_out = "that is in the alphabet" #GUI output text change for user to see
                root.update_idletasks() #failed attemped to refresh screen
                alphabet.remove(str(guess_var.get())) #removes choice after use and guess goes through

        elif len(str(guess_var.get()))>1: #result if input is too long
            print("too many chaactar")


    w = Button (root,command=Guess, text="Check").place(x=850, y=300) #submit button for users guess from entry bar


    root.mainloop()

【问题讨论】:

    标签: python-3.x user-interface tkinter


    【解决方案1】:

    您需要定义一个 tkinter StringVar,并使用它来设置标签中 textvariable 的值。
    下面向您展示了标签更新如何与输入框中的字母一起使用,这回答了您的问题。 (它不会解决您代码中的其他问题)。

    import tkinter as tk
    import random
    
    
    def guess():
        global guess_out
        guessed = guess_bar.get()
        print(guessed)
        if len(guessed) == 1:
            print("one char")
            if str(guessed) in alphabet:
                guess_out.set(guessed)
                alphabet.remove(guessed)
    
        elif len(str(guess_var.get())) > 1:
            print("too many characters")
    
    
    if __name__ == '__main__':
    
        root = tk.Tk()
        root.wm_title("Hangman")
    
        w = tk.Canvas(root, width=1000, height=650)
    
        w.create_rectangle(250, 0, 450, 25, fill="brown")
        w.create_rectangle(250, 0, 275, 100, fill="brown")
        w.create_rectangle(475, 0, 450, 400, fill="brown")
        w.create_rectangle(300, 400, 600, 425, fill="brown")
        w.pack()
    
        alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
        words = ["atmosphere", "miscellaneous", "muffins", "junk", "pajamas", "engineering", "privilege", 
                 "amplifier", "countryside", "python", "magic", "hats", "flash", "clock", "ceiling"]
    
        word_choice = random.choice(words)
        mult = len(word_choice)
        underscores = "_ " * mult
        w.create_text(500, 600, font=("Purisa", 48), text=underscores)
    
        guess_out = tk.StringVar()
        guess_out.set("$")
    
        text_out = tk.Label(root, font=("Purisa", 18), textvariable=guess_out)
        text_out.place(x=650, y=200)
    
        guess_bar_label = tk.Label(root, font=("Purisa", 20), text="Guess: ")
        guess_bar_label.place(x=600, y=285)
    
        guess_bar = tk.Entry(root)
        guess_bar.place(x=700, y=300)
    
        w = tk.Button(root, command=guess, text="Check")
        w.place(x=850, y=300)
    
        root.mainloop()
    

    【讨论】:

      【解决方案2】:

      您可以创建一个按钮来删除当前显示所在的框架。删除后,在父框架中绘制一个新框架并使用刷新的框架添加所有新代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-14
        • 1970-01-01
        • 1970-01-01
        • 2021-11-10
        • 1970-01-01
        • 2021-05-19
        • 1970-01-01
        • 2013-10-09
        相关资源
        最近更新 更多