如何刷新tkinter中的屏幕以显示新的标签文本?

时间:2018-06-11 04:10:49

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

下面是我的代码,我正在为自己开发一个有趣的刽子手游戏,习惯于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()

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您需要定义一个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()