Python Tkinter表示将元素作为标签排队

2024-05-15 23:16:45 发布

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

我有一个Tkinter应用程序,它不能基于我的队列值生成标签。每个标签都包含产品详细信息。每个队列元素都是从客户订单的数据库表中获取的。但是,我需要一个按钮来按complete,它会弹出队列项,并从窗口中删除标签。我几乎得到了它的工作,按钮与第一个标签工程,并没有删除标签后,这个

我还没有包括最小方法的队列代码

conn=sqlite3.connect("system.db")
cur=conn.cursor()
query = cur.execute("""SELECT orderid, product, size, quantity, milkOptions FROM 
activeCustomerOrders""").fetchall()
conn.commit()
conn.close()

customerQueue = Queue()
for row in query:
    customerQueue.enqueue(row)



class MyFirstGUI:
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        self.label = Label(master, text="This is our first GUI!")
        self.label.pack()

        self.completedButton = Button(master,text="Complete",width=30,height=5,bg="green")
        self.completedButton.pack(side=BOTTOM)
        self.completedButton.bind('<Button-1>', 
        self.orderFulfilled)



        for item in customerQueue.queue:
        self.button = Label(master,text=item,width=30,height=5,bg="red")
        self.button.pack(side=LEFT)


    def orderFulfilled(self, event):
        #print("hi")
        customerQueue.dequeue()

        for item in customerQueue.queue:
            self.button.pack_forget()
            #self.button = 
         Label(self.master,text=item,width=30,height=5,bg="red")

root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
+---------+-----------+--------+-------------+------------+----------+-------+------------+
| orderid |  product  |  size  | milkOptions | orderDate  | quantity | price | customerid |
+---------+-----------+--------+-------------+------------+----------+-------+------------+
|       1 | Espresso  | Small  | Soya        | 2019-10-29 |        1 | 1.0   |        1   |
|       2 | Cappucino | Small  | SemiSkimmed | 2019-10-29 |        1 | 1.0   |        1   |
|       3 | Cappucino | Small  | SemiSkimmed | 2019-10-29 |        1 | 1.0   |        1   |
|       4 | Cappucino | Medium | SemiSkimmed | 2019-10-29 |        1 | 1.0   |        1   |
+---------+-----------+--------+-------------+------------+----------+-------+------------+

Tags: textinselfmasterfor队列button标签
1条回答
网友
1楼 · 发布于 2024-05-15 23:16:45

看看https://stackoverflow.com/help/minimal-reproducible-example,它使用list跟踪标签:

#! python3
import sys
from tkinter import *

queue = "one two three".split()


class MyFirstGUI:
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        self.label = Label(master, text="This is our first GUI!")
        self.label.pack()

        self.completedButton = Button(master,text="Complete",width=30,height=5,bg="green")
        self.completedButton.pack(side=BOTTOM)
        self.completedButton.bind('<Button-1>', self.orderFulfilled)

        self.items = [] 
        for item in queue:
            button = Label(master,text=item,width=30,height=5,bg="red")
            button.pack(side=LEFT)
            self.items.append(button)

    def orderFulfilled(self, event):
        # for item in queue:
            button = self.items.pop(-1)
            button.pack_forget()
            # Label(self.master,text=item,width=30,height=5,bg="red")

root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()

相关问题 更多 >