【问题标题】:Comunicate between main GUI interface and top level GUI interface在主 GUI 界面和顶级 GUI 界面之间进行通信
【发布时间】:2020-11-25 15:05:33
【问题描述】:

我在 GUI 的通信之间遇到了问题。我的程序包含一个主界面,该界面创建于 class maininterface(): 和其他在不同类中创建的顶级接口: class Interface_PowerBalance()。主界面控制其他顶级界面。我的问题是我不想将顶级接口的某些属性的信息接收到主界面中并且我没有实现它。

这里是 class maininterface(): 我创建主 GUI 的地方。

class mainInterface():

    def __init__(self,root,title):
        self.root=root
        self.root.title(title)
        self.Widgets()
    def Widgets(self):
        self.PowerBalanceLabel=Label(self.root,text='Power balance is:')
        self.PowerBalanceLabel.grid(row=0,column=1)

        self.Button_InterfacePowerBalance=Button(root,command=self.PowerBalanceframe)
        self.Button_InterfacePowerBalance.grid(column=0,row=0)
        
    def PowerBalanceframe(self):
        self.Power=Interface_PowerBalance(self.root,'Power balance interface').PowerBalance()
        self.PowerBalanceLabel.config(text='Power balance is: '+str(self.Power))

当我单击self.Buttton_InterfacePowerBalance 时,程序会创建位于class Interface_PowerBalance() 中的顶级接口,并带有以下代码:

class Interface_PowerBalance():
    """ This class creates the interface to obtain the power balance """
    def __init__(self,root,title):
        self.root=root
        self.PowerBalanceFrame=Toplevel(self.root)
        self.PowerBalanceFrame.title(title)
        self.value=DoubleVar()
        self.valueList=[]
        self.Widget()
    def Widget(self):
        self.myentry_value = Entry(self.PowerBalanceFrame,textvariable=self.value)
        self.myentry_value.grid(row=2,column=2)
        self.Button1=Button(self.PowerBalanceFrame, text='Insert a device', command=self.StoreData)
        self.Button1.grid(row=0,column=0)
    def StoreData(self):
        self.valueList.append(self.value.get())
        self.PowerBalance()
    def PowerBalance(self,*args):
        self.Power=float(self.Power)
        N=len(self.valueList)
            for j in range (0,N):
                self.Power=self.Power+float(self.valueList[j])
        self.PowerLabel=Label(self.PowerBalanceFrame,text=str(self.Power))
        self.PowerLabel.grid(row=6,column=2)
        return self.Power

我的程序现在要做的就是在这个顶级界面打开时从class Interface_PowerBalance():接收self.Power属性,并在self.PowerBalanceLabel的主界面中显示它。 我想要做的是从class Interface_PowerBalance():接收self.Power属性每次更改此属性时并将其显示在self.PowerBalanceLabel的主界面中。

我不知道这是否有意义。

谢谢。

【问题讨论】:

    标签: python class oop tkinter toplevel


    【解决方案1】:

    首先在mainInterface中创建一个变量幂,因为你要一直跟踪它:

    class mainInterface():
        def __init__(self,root,title):
            self.root=root
            self.root.title(title)
            self.Widgets()
            self.Power = 0.0 # Here you will store the Power value
    

    第二,你不会改变PowerBalanceFrame方法中标签的值,因为你没有绑定toplevel知道它什么时候关闭,所以把它改成:

    def PowerBalanceframe(self):
        Interface_PowerBalance(self,'Power balance interface').PowerBalance()
    

    现在的诀窍在于如何创建toplevel。您希望它编辑主界面的标签,因此您必须将 main 作为参数传递(请参阅我将self 作为参数而不是self.root 传递),因此您必须更改Interface_PowerBalance__init__

    class Interface_PowerBalance():
        """ This class creates the interface to obtain the power balance """
        def __init__(self,main,title):
            self.main = main # here main is the main interface
            self.root=main.root # thus main.root is the root
            self.PowerBalanceFrame=Toplevel(self.root)
            self.PowerBalanceFrame.title(title)
            self.value=DoubleVar()
            self.valueList=[]
            self.Widget()
    

    现在您可以访问main.Powermain.PowerBalanceLabel,因此只需将PowerBalance 方法更改为:

    def PowerBalance(self,*args):
        N=len(self.valueList)
        for j in range (0,N):
            self.main.Power=self.main.Power+float(self.valueList[j])
        self.PowerLabel=Label(self.PowerBalanceFrame,text=str(self.main.Power))
        self.PowerLabel.grid(row=6,column=2)
        self.main.PowerBalanceLabel['text'] = "Power balance is: " + str(self.main.Power) #here is where you edit the main GUI
    

    这会起作用,但我会警告你,你的代码不是很好。首先,如果您将mainInterface 用作tk.Tk 的子代,那就更好了。其次,您可以使用 tkinter 变量来跟踪电源并使用 bind 来识别它何时更改。最后但同样重要的是,变量应该是小写的,类应该是大写的。

    【讨论】:

    • 谢谢你,这对我有帮助。当你说我应该使用mainInterface 作为tk.Tk 的孩子时,你是什么意思?
    • 看看 Python 继承。如果您通过创建大型机来执行class MainFrame(tk.Tk),您将自动创建主窗口,因此您无需执行root=tk.Tk() 并将根传递给大型机,因为大型机自身将成为根
    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多