【发布时间】:2014-09-28 11:13:41
【问题描述】:
我正在学校做一个编码项目,我正在尝试为我的按钮设置功能 但是当我运行代码时,按钮会在不被点击的情况下运行命令,并且它们不会消失,因为隐藏它们的函数中提到的按钮不起作用:
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()
【问题讨论】:
-
此代码未运行(调用
Buttons时缺少参数)。请发布您正在运行的实际代码。坚持Python naming guidelines也很好。 -
您的代码在做一些根本错误的事情,这可能会导致几种类型的不良副作用。您永远不应创建多个
Tk的实例。如果需要多个窗口,则需要创建Toplevel的实例。
标签: python user-interface tkinter tk