使用 tkinter 模块在 python 中制作菜单(餐厅菜单)
making a menu(restaurant menu) in python with the tkinter module
将 tkinter 与 python 结合使用,我正在制作一个菜单,显示您输入的内容。然后可以通过按下按钮将其删除,我的问题是如何以正确的顺序删除标签。顺序应该是从上到下。 “订单”按日期显示,最旧的在顶部,最新的在列表底部。通过使用 destroy(),我只能设法删除最后一个标签(最新的)。我需要帮助来弄清楚如何删除特定标签。
#imports-----------------------
from tkinter import *
#from PIL import ImageTk,image
from tkinter import messagebox
import time
#imports----------------------
#start-----------
x=1
height=0
l="0"
root = Tk()
root.geometry("300x200")
#start-----------
def myClick():
global x #number of the label
global height
global l #name of the label
global vartoprint
global y #to retain the old value of x
if x == 1:
global statehold
statehold=1
l=str(statehold)
vartoprint="order one"
y=1
if x > y: #when x is different than the old value of y, we assume the state has gone up
#and therefor a new entry has come, time to print this new value
height=height+1
statehold= statehold + 1
l=str(statehold)
vartoprint="order 'xxx'" #in this case, the new value is always "order xxx" for testing
l = Label(root, text=vartoprint)
l.grid(row=height, column=L)
x=x+1
mybutton = Button(root, text="next", padx=10, pady=8, command=myClick, fg="black", bg="white")
mybutton.grid(row=1, column=5)
def mydelete():
global statehold
global x
global l
statehold= statehold - 1 #tells the system that 1 label has been removed
l.destroy() #destroys the label
x=x-1 #tells the system that 1 label has been removed
DeleteButton = Button(root, text="next and delete", command=mydelete)
DeleteButton.grid(row=1, column=6)
#end-------------
time.sleep(3)
root.mainloop()
#end-------------
感谢使用列表并删除第一个列表项,我能够完成我的任务。
我用
my_list.append(l)
在列表末尾插入变量。
就像你说的,我使用
(my_list.pop(0)).destroy()
删除列表中的第一项。
谢谢,你太棒了!
将 tkinter 与 python 结合使用,我正在制作一个菜单,显示您输入的内容。然后可以通过按下按钮将其删除,我的问题是如何以正确的顺序删除标签。顺序应该是从上到下。 “订单”按日期显示,最旧的在顶部,最新的在列表底部。通过使用 destroy(),我只能设法删除最后一个标签(最新的)。我需要帮助来弄清楚如何删除特定标签。
#imports-----------------------
from tkinter import *
#from PIL import ImageTk,image
from tkinter import messagebox
import time
#imports----------------------
#start-----------
x=1
height=0
l="0"
root = Tk()
root.geometry("300x200")
#start-----------
def myClick():
global x #number of the label
global height
global l #name of the label
global vartoprint
global y #to retain the old value of x
if x == 1:
global statehold
statehold=1
l=str(statehold)
vartoprint="order one"
y=1
if x > y: #when x is different than the old value of y, we assume the state has gone up
#and therefor a new entry has come, time to print this new value
height=height+1
statehold= statehold + 1
l=str(statehold)
vartoprint="order 'xxx'" #in this case, the new value is always "order xxx" for testing
l = Label(root, text=vartoprint)
l.grid(row=height, column=L)
x=x+1
mybutton = Button(root, text="next", padx=10, pady=8, command=myClick, fg="black", bg="white")
mybutton.grid(row=1, column=5)
def mydelete():
global statehold
global x
global l
statehold= statehold - 1 #tells the system that 1 label has been removed
l.destroy() #destroys the label
x=x-1 #tells the system that 1 label has been removed
DeleteButton = Button(root, text="next and delete", command=mydelete)
DeleteButton.grid(row=1, column=6)
#end-------------
time.sleep(3)
root.mainloop()
#end-------------
感谢使用列表并删除第一个列表项,我能够完成我的任务。
我用
my_list.append(l)
在列表末尾插入变量。 就像你说的,我使用
(my_list.pop(0)).destroy()
删除列表中的第一项。
谢谢,你太棒了!