我正在学校做一个编码项目,我正在尝试为我的按钮设置功能 但是当我运行代码时,按钮运行命令而不点击它们并且它们不会消失,因为隐藏它们的函数中提到的按钮不起作用:
from tkinter import *
import tkinter
#Parent Window Setup
GoodChefWindow = Tk()
GoodChefWindow.title("Welcome To GoodChef")
GoodChefWindow.geometry("600x500")
#Displaying Company Logo
Logo=PhotoImage(file="LogoV2.gif")
LogoCanvas=tkinter.Canvas(GoodChefWindow,height=150, width=600)
LogoCanvas.create_image(300,75,image=Logo)
LogoCanvas.pack()
#Pickup and Delivery buttons
def Delivery_Clicked():
Delivery=Tk()
Delivery.title("Delivery Options")
Delivery.geometry("400x333")
Remove_Buttons()
def PickUp_Clicked():
Delivery=Tk()
Delivery.title("Pick Up Options")
Delivery.geometry("400x333")
Remove_Buttons()
def Remove_Buttons():
MenuButtonsFrame.destroy()
def Buttons(MenuButtonsFrame):
DeliveryIcon=PhotoImage(file="Delivery_Icon.gif")
PickUpIcon=PhotoImage(file="Pick_Up_Icon.gif")
MenuButtonsFrame = LabelFrame(GoodChefWindow, text="Order")
MenuButtonsFrame.pack()
DeliveryLabelFrame = LabelFrame(MenuButtonsFrame)
DeliveryLabelFrame.pack(side=RIGHT,expand="yes")
PickUpLabelFrame = LabelFrame(MenuButtonsFrame)
PickUpLabelFrame.pack(side=LEFT,expand="yes")
DeliveryButton = tkinter.Button(DeliveryLabelFrame,bg="red",
fg="yellow",compound="left",
image=DeliveryIcon,height=40,
width=120,text="Delivery",
command=Delivery_Clicked())
PickUpButton = tkinter.Button(PickUpLabelFrame,bg="red",
fg="yellow",compound="left",
image=PickUpIcon,height=40,
width=120,text="Pick Up",
command=PickUp_Clicked())
DeliveryButton.pack()
PickUpButton.pack()
Buttons()
答案 0 :(得分:0)
在我看来,你真的需要重新思考你的方法,这就是我到目前为止所做的事情:
from tkinter import *
#Parent Window Setup
GoodChefWindow = Tk()
GoodChefWindow.title("Welcome To GoodChef")
GoodChefWindow.geometry("600x500")
#Displaying Company Logo
Logo=PhotoImage(file="rd.gif")
LogoCanvas=Canvas(GoodChefWindow,height=150, width=600)
LogoCanvas.create_image(300,75,image=Logo)
LogoCanvas.pack()
#Pickup and Delivery buttons
def Delivery_Clicked():
Delivery=Toplevel()
Delivery.title("Delivery Options")
Delivery.geometry("400x333")
Remove_Buttons(MenuButtonsFrame)
def PickUp_Clicked():
Delivery=Toplevel()
Delivery.title("Pick Up Options")
Delivery.geometry("400x333")
Remove_Buttons(MenuButtonsFrame)
def Remove_Buttons(MenuButtonsFrame):
MenuButtonsFrame.destroy()
DeliveryIcon=PhotoImage(file="rd.gif")
PickUpIcon=PhotoImage(file="rd.gif")
MenuButtonsFrame = LabelFrame(GoodChefWindow, text="Order")
MenuButtonsFrame.pack()
DeliveryLabelFrame = LabelFrame(MenuButtonsFrame)
DeliveryLabelFrame.pack(side=RIGHT,expand="yes")
PickUpLabelFrame = LabelFrame(MenuButtonsFrame)
PickUpLabelFrame.pack(side=LEFT,expand="yes")
DeliveryButton = Button(DeliveryLabelFrame,bg="red",
fg="yellow",compound="left",
image=DeliveryIcon,height=40,
width=120,text="Delivery",
command=Delivery_Clicked)
PickUpButton = Button(PickUpLabelFrame,bg="red",
fg="yellow",compound="left",
image=PickUpIcon,height=40,
width=120,text="Pick Up",
command=PickUp_Clicked)
DeliveryButton.pack()
PickUpButton.pack()
mainloop()
这应该有帮助吗?如果不说出来!你也可以制作更多的tk窗户(顶层是不必要的)。