【问题标题】:Python Tkinter representing a queues elements as labelsPython Tkinter 将队列元素表示为标签
【发布时间】:2019-11-04 10:57:18
【问题描述】:

我有一个 Tkinter 应用程序,它不会根据我的队列值生成标签。每个标签都包含产品详细信息。每个队列元素都是从我的客户订单数据库表中获取的。但是,我需要一个按钮来按下完成,它会弹出队列项目,并从窗口中删除标签。我几乎可以使用它,该按钮适用于第一个标签,此后不再删除标签。

我没有包含极简主义方法的队列代码。

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   |
+---------+-----------+--------+-------------+------------+----------+-------+------------+

【问题讨论】:

  • 你的类中只有一个按钮元素 (self.button),而且它似乎没有与任何处理程序相关联,例如 self.completedButtenis。

标签: python python-3.x tkinter queue


【解决方案1】:

看看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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-29
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多