python-3.x Tkinter程序中的循环标签

t5zmwmid  于 11个月前  发布在  Python
关注(0)|答案(2)|浏览(94)

当你点击这个按钮时,它会显示一个标签列表。如何创建一个标签输出并在点击时更新它

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()

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

8yoxcaq7

8yoxcaq71#

你的代码是如此整洁,继续前进!你必须知道你不需要一个while循环,因为每次你点击按钮(好的)“display_text”函数都会重新启动,所以首先,你应该删除while循环,然后用if语句替换while循环,检查try是否小于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()

字符串

agyaoht7

agyaoht72#

如果你想在点击按钮时显示猜测结果,你不应该在display_text()内部使用while循环。你还需要在函数外部初始化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()

字符串

相关问题