Tkinter 程序中的循环标签

Loop label in Tkinter Program

提问人:Vladyslav Bosyi 提问时间:11/17/2023 更新时间:11/20/2023 访问量:58

问:

当您单击该按钮时,它会显示一个向下的标签列表。如何制作一个标签输出并在单击时更新它

import random
from tkinter import *
from tkinter import ttk

win= Tk()

win.geometry("1920x1720")
def display_text():
    number = random.randint(1, 101)
    tries = 0
    while tries < 10:
        global entry
        string = entry.get()
        tries += 1
        if int(string) > number:
            b = Label(win, text="Your number more than mine", font=("Courier 22 bold"))
            b.pack()
            triess = "Your attempts " + str(tries)
            v = Label(win, text=triess, font=("Courier 22 bold"))
            v.pack()
        if int(string) < number:
            c = Label(win, text="Your number less than mine", font=("Courier 22 bold"))
            c.pack()
            triess = "Your attempts " + str(tries)
            v = Label(win, text=triess, font=("Courier 22 bold"))
            v.pack()
        if int(string) == number:
            break
    if int(string) == number:
        tries = str(tries)
        d = Label(win, text="Congratulation, you're win", font=("Courier 22 bold"))
        d.pack()
        v = Label(win, text=triess, font=("Courier 22 bold"))
        v.pack()
    if int(string) != number:
        number = str(number)
        d = Label(win, text="Unfortunately, you're loser", font=("Courier 22 bold"))
        d.pack()
        v = Label(win, text=triess, font=("Courier 22 bold"))
        v.pack()

entry= Entry(win, width= 40)
entry.focus_set()
entry.pack()
ttk.Button(win, text= "Okay",width= 20, command= display_text).pack(pady=20)

win.mainloop()

我试图改变代码任何部分的位置,但都是徒劳的

蟒蛇 python-3.x tkinter

评论


答:

0赞 acw1668 11/17/2023 #1

如果要在单击按钮时显示猜测结果,则不应在内部使用 while 循环。此外,您还需要初始化函数并在函数外部:display_text()triesnumber

# initialize tries and number
tries = 0
number = random.randint(1, 101)

def display_text():
    # declare tries as global variable
    global tries

    if tries >= 10:
        # game is over
        return

    try:
        guess = int(entry.get())
    except ValueError:
        errmsg = Label(win, text="Invalid number", font=("Courier 22 bold"), fg="red")
        errmsg.pack()
        # remove the error message one second later
        errmsg.after(1000, errmsg.destroy)
        return

    tries += 1
    if guess == number:
        d = Label(win, text="Congratulation, you win!", font=("Courier 22 bold"))
        d.pack()
        tries = 10  # game over
    else:
        msg = f"Your number {guess} is {'more' if guess > number else 'less'} than mine"
        b = Label(win, text=msg, font=("Courier 22 bold"))
        b.pack()
        triess = "Your attempts " + str(tries)
        v = Label(win, text=triess, font=("Courier 22 bold"))
        v.pack()
        if tries >= 10:
            d = Label(win, text=f"Unfortunately, you lose.\nThe number is {number}", font=("Courier 22 bold"))
            d.pack()
1赞 Marwan Rabi 11/20/2023 #2

你的代码是如此整洁,继续前进!你必须知道你不需要while循环,因为每次你点击按钮(好的)“display_text”函数都会重新启动,所以首先,你应该删除while循环,然后用if语句替换while循环,检查尝试次数是否小于10次,然后执行你的代码。我将把编辑后的代码和下面的注释放在一起。 请自己尝试这些提示,然后检查代码...感谢您阅读我的解释。 代码如下:

#importing files
import random
from tkinter import *
from tkinter import ttk

#The root 
win= Tk()
#The auto dimension of the window
win.geometry("1920x1720")
#defining the tries of the play
tries = 0
#function that displays if the user wins or loses and records attempts
def display_text():
    #taking the random number
    number = random.randint(1, 101)
    #Global the variables of the number of tries 
    global tries
    global triess
    #check if the user have more tries or he finished all tries 
    if tries < 10:
        #Globaling the entry variable that contains the box to be used out of the function
        global entry
        #Taking the value of the user try
        string = entry.get()
        #check if the code is more than the random number
        if int(string) > number:
            #printing on screen
            b = Label(win, text="Your number more than mine", font=("Courier 22 bold"))
            #showing the label on the screen
            b.pack()
            #define a variable to be used in showing the user the number of attempts and storage the number of tries in it
            triess = "Your attempts " + str(tries)
            #printing on screen
            v = Label(win, text=triess, font=("Courier 22 bold"))
            #showing on screen
            v.pack()
        #check if the code is less than the random number
        if int(string) < number:
            ## All the codes are explained in the previous if statment
            c = Label(win, text="Your number less than mine", font=("Courier 22 bold"))
            c.pack()
            triess = "Your attempts " + str(tries)
            v = Label(win, text=triess, font=("Courier 22 bold"))
            v.pack()
        #check if the code is equal to the random number
        if int(string) == number:
            #The number of tries has been converted into a string in order to print it
            tries = str(tries)
            d = Label(win, text="Congratulation, you're win", font=("Courier 22 bold"))
            d.pack()
            v = Label(win, text=triess, font=("Courier 22 bold"))
            v.pack()
        #the if statement checks if the user has won to stop increasing the number of tries 
        if int(string) == number:
            pass
        else:
            tries += 1
    #in case there aren't more tries
    else:
        d = Label(win, text="Unfortunately, you're loser", font=("Courier 22 bold"))
        d.pack()
        v = Label(win, text=triess, font=("Courier 22 bold"))
        v.pack()

#The box which the user will write in int his prediction
entry= Entry(win, width= 40)
entry.focus_set()
entry.pack()
#button to enter the prediction
ttk.Button(win, text= "Okay",width= 20, command= display_text).pack(pady=20)

win.mainloop()