【问题标题】:Making buttons disappear in Tkinter使按钮在 Tkinter 中消失
【发布时间】: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


【解决方案1】:

在我看来,你真的需要重新考虑你的方法,这是我迄今为止所做的事情:

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 窗口(不需要顶层)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2019-07-06
    • 2020-01-14
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多