【问题标题】:Editing GUI from another GUI after run-time in Tkinter在 Tkinter 中运行后从另一个 GUI 编辑 GUI
【发布时间】:2020-04-19 22:54:45
【问题描述】:

我有两个类产生两个单独的 GUI 窗口。我正在努力实施一种情况,例如如果在第一个 GUI 中按下按钮,它会在运行时之后将标签添加到第二个 GUI。有人可以为我提供解决方案吗?

Class CustomerOrder:
    def __init__(self, master):
        self.master = master
        master.title("Customer Order GUI")

        self.completedButton1 = Label(master,text=" Place Order:")
        self.completedButton1.pack(side=TOP)

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



class baristaPage(tk.Frame):

    def __init__(self, master):
        self.master = master
        master.title("baristaPage")

        self.baristaPage = Label(text="Barista Page")
        self.baristaPage.place(x=0,y=0)

        dashboard = Label(text="Customer Queue System")
        dashboard.place(x=0,y=80)

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

【问题讨论】:

  • 当您想要拥有多个 GUI 窗口时,一个好的方法是为每个窗口创建 tk.Toplevel 小部件(并且只在脚本的主要部分调用 tk.Tk() 一次)。在这种情况下,您可以在创建类的实例时将它们作为 master 传递(而不是 root)。
  • 对不起。我不认为你理解这个问题。我想要两个单独的 GUI,因为它们在概念上将显示在商店内的两个单独的监视器上。例如一台显示器供客户使用,一台显示器供咖啡师使用
  • 只要代码在同一个 CPU 上运行(即使 2 个 GUI 窗口显示在 2 个不同的监视器上),最好对主窗口进行一次调用 Tk()(说,给咖啡师的那个),然后打电话给Toplevel() 给另一个(给顾客的那个)。
  • 您是否希望这段代码在不同机器上的不同进程中运行?它的格式化方式看起来像是您试图在一个进程中创建两个窗口。

标签: python python-3.x user-interface tkinter tkinter-layout


【解决方案1】:

以下是创建 Barista 和 Customer 窗口的示例代码。 Customer 窗口包含一个 Button,每次按下此按钮时,它都会增加订单计数器并更新 Barista 窗口。这是你需要的那种东西吗?

from tkinter import *

class Customer(Toplevel):

    def __init__(self, master):
        Toplevel.__init__(self) # create the secondary window
        self.title("Customer")
        self.master = master
        self.counter = 0

        self.customer = Label(self, text="Customer Page", width=40)
        self.customer.pack(side=TOP)

        self.button = Button(self, text="Place Order:", command=self.order)
        self.button.pack(side=TOP)

        self.mainloop()

    def order(self):
        self.counter += 1
        self.master.dashboard['text'] = "Order number %s" % self.counter


class Barista(Tk):

    def __init__(self):
        Tk.__init__(self) # create the main window
        self.title("Barista")

        self.barista = Label(self, text="Barista Page", width=40)
        self.barista.pack(side=TOP)

        self.dashboard = Label(self, text="Customer Queue System")
        self.dashboard.pack(side=TOP)

        self.customer = Customer(self) # instantiate Customer window
        self.mainloop()

Barista()

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多