如何在按下按钮之前隐藏python中的标签

2024-05-20 22:49:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个flashcard应用程序,希望在用户得到正确答案时弹出一个标签。我想知道如何使这两个标签都隐藏起来,直到按下其中一个按钮,然后只有一个会出现(最好在按下下一个按钮时消失)。我的代码如下

from tkinter import *
from PIL import ImageTk, Image
from random import randint
import random


root = Tk()
root.title('Chemistry Flashcards')
root.geometry("500x500")

def balancing():
    hide_all_frames()
    balancing_frame.pack(fill="both", expand=1)




    global show_balancing
    show_balancing = Label(balancing_frame)
    show_balancing.pack(pady=15)

    global balancing_list
    balancing_list = ['balanced1', 'balanced2', 'balanced3', 'balanced4', 'balanced5', 'unbalanced1', 'unbalanced2', 'unbalanced3', 'unbalanced4', 'unbalanced5']

    global balanced_list
    balanced_list = balancing_list[:5]


    global unbalanced_list
    unbalanced_list = balancing_list[5:10]


    global rando_image
    rando_image = random.choice(balancing_list)

    global balancing_image
    balancing1 = "C:/Users/Kisitu/Desktop/project/balancing/" + rando_image + ".png"
    balancing_image = ImageTk.PhotoImage(Image.open(balancing1))
    show_balancing.config(image=balancing_image)



    global balanced_button
    balanced_button = Button(balancing_frame, text = 'balanced', command = balancing_answer).pack()

    global unbalanced_button
    unbalanced_button = Button(balancing_frame, text = 'unbalanced', command = balancing_answer).pack()

    global balanced_label
    balanced_label = Label(balancing_frame, text='It was balanced', font=("Helvetica",18), bg='#B3FDFF')
    balanced_label.pack(pady=15)

    global unbalanced_label
    unbalanced_label = Label(balancing_frame, text='It was unbalanced', font=("Helvetica",18), bg='#B3FDFF')
    unbalanced_label.pack(pady=15)






    balancing()



def hide_all_frames():

    for widget in balancing_frame.winfo_children():
        widget.destroy()

    balancing_frame.pack_forget()


balancing_frame = Frame(root, width=500, height=500, )


my_menu = Menu(root)
root.config(menu=my_menu, bg='#B7F7BB')

#menu options(elements and compound)
lesson_menu = Menu(my_menu)
my_menu.add_cascade(label="Lesson", menu=lesson_menu)
lesson_menu.add_command(label="balancing", command=balancing)
lesson_menu.add_separator()
lesson_menu.add_command(label="Exit", command=root.quit)


'''
end
'''
root.mainloop()

Tags: imageimportshowrootglobalframelabelcommand
2条回答
from tkinter import *
from PIL import ImageTk, Image
from random import randint
import random
root = Tk()
root.title('Chemistry Flashcards')
root.geometry("500x500")


def balancing_answer():
    balancing_frame.pack()
    balanced_label.pack(pady=15)
    unbalanced_label.pack_forget()
    show_balancing.config(image=balancing_image)


def unbalancing_answer():
    balancing_frame.pack_forget()
    balanced_label.pack_forget()
    unbalanced_label.pack(pady=15)
    show_balancing.config(image=balancing_image)


#two frames-unbalancing frame and balancing frame
balancing_frame = Frame(root)
unbalancing_frame=Frame(root)

#this is the show balancing frame
show_balancing = Label(balancing_frame)
show_balancing.pack()

#only one image is specified. there can be no random images.but the image will flash up at a click and disappear at another click
img = Image.open('p.jpg').convert("RGBA")
w, h = img.size
left = w/5
right = 3*w/5
upper = h/5
lower = 3*h/5
img2 = img.crop([ left, upper, right, lower]) #this part is used to crop the image. you can choose to ignore
balancing_image= ImageTk.PhotoImage(img2)



#two buttons to click

balanced_button = Button(unbalancing_frame,       text = 'balanced', command = balancing_answer).pack()
unbalanced_button = Button(unbalancing_frame, text = 'unbalanced', command = unbalancing_answer).pack()



#the two labels balanced and unbalanced
balanced_label = Label(balancing_frame, text="it was balanced", font=("Helvetica",9), bg='#B3FDFF')
unbalanced_label = Label(unbalancing_frame, text='It was unbalanced', font=("Helvetica",9), bg='#B3FDFF')


#when the user click balancing.. a new frame appear
def balancing():
    unbalancing_frame.pack()


#========the menu=========

my_menu = Menu(root)
root.config(menu=my_menu, bg='#B7F7BB')
#menu options(elements and compound)
lesson_menu = Menu(my_menu)
my_menu.add_cascade(label="Lesson", menu=lesson_menu)
lesson_menu.add_command(label="balancing", command=balancing)
lesson_menu.add_separator()
lesson_menu.add_command(label="Exit", command=root.quit)



root.mainloop()

向两个按钮添加功能

def balancing_answer(): #this will make the label to show and hide
    balanced_label.pack(pady=15)
    unbalanced_label.pack_forget()

def unbalancing_answer(): #this will make the label to show and hide
    balanced_label.pack_forget()
    unbalanced_label.pack(pady=15)



global balanced_button
balanced_button = Button(balancing_frame, text = 'balanced', command = balancing_answer).pack()

global unbalanced_button
unbalanced_button = Button(balancing_frame, text = 'unbalanced', command = unbalancing_answer).pack() # change the command to unbalancing_answer

global balanced_label
    balanced_label = Label(balancing_frame, text='It was balanced', font=("Helvetica",18), bg='#B3FDFF')


global unbalanced_label
    unbalanced_label = Label(balancing_frame, text='It was unbalanced', font=("Helvetica",18), bg='#B3FDFF')

#    balancing()  # no need for this

如果我没有弄错你的问题;这将使标签弹出并消失

相关问题 更多 >