简体   繁体   English

tkinter:如何从不同的 class 访问 StringVar 和对象?

[英]tkinter: How to access a StringVar and objects from a different class?

I'm having a simple tkinter two frame application with a Label , Entry and Button widget and I want to access a StringVar() of FrameOne with a Entry and Button of FrameTwo.我有一个简单的 tkinter 两帧应用程序,带有LabelEntryButton小部件,我想使用FrameOneEntryButton访问 FrameOne 的StringVar() FrameTwo.

If have seen a lots of examples of code, but do not get how this is been done in my example below.如果已经看过很多代码示例,但在下面的示例中不明白这是如何完成的。 Many programmers are using a controller.许多程序员都在使用 controller。 If I would use a controller, I end up from an error to another.如果我使用 controller,我最终会从一个错误到另一个错误。 For example:例如:

FirstFrame = FrameOne(mainWindow)`

TypeError: __init__() missing 1 required positional argument: 'controller'

Which I completely understand, because I do not pass anything into the new 'controller' class argument when calling the Frame class.我完全理解,因为在调用 Frame class 时,我没有将任何内容传递给新的“控制器”class 参数。 But I do not know what I should pass into this to solve it.但我不知道我应该传递什么来解决它。 Perhaps it is also caused by the lack of knowledge of using class variables (any literature tips are welcome).也许这也是由于缺乏使用 class 变量的知识造成的(欢迎任何文献提示)。

The same counts for the solution to inherit FrameOne into FrameTwo .FrameOne继承到FrameTwo的解决方案也同样重要。 I bump into the same amount of errors applying to my code.我遇到了适用于我的代码的相同数量的错误。

Another thing is that many programmers have examples of two frames that are not visible at the same time, while in my example I have two frames underneath each other at the same time.另一件事是,许多程序员都有两个同时不可见的框架的示例,而在我的示例中,我同时有两个框架在彼此下方。

An different related issue that I have is, what if the label widget of FrameOne was a Text widget?我遇到的另一个相关问题是,如果 FrameOne 的FrameOne小部件是Text小部件怎么办? How do I access the widget from FrameTwo .如何从FrameTwo访问小部件。

I could make it work with globals, but I do not want to use such writing and I will keep the access widget problem anyhow.我可以让它与全局变量一起工作,但我不想使用这样的写作,无论如何我都会保留访问小部件的问题。

Please find my code below:请在下面找到我的代码:

import tkinter as tk

class AppWindow():
    def __init__(self, master):
        self.master = master
        master.title("Test Application")
        master.geometry("1060x680")
        master.grid_propagate(False)


class FrameOne(tk.Frame):
    def __init__(self, parent):
        super().__init__()
        self["borderwidth"]=5
        self["relief"]="ridge"

        self.LabelText = tk.StringVar()
        self.LabelText.set("It is not working yet")

        self.testlabel = tk.Label(self, textvariable=self.LabelText)        
        self.testlabel.grid(row=1, column=1)


class FrameTwo(tk.Frame):
    def __init__(self, parent):
        super().__init__()
        self["borderwidth"]=5
        self["relief"]="ridge"

        self.testentry = tk.Entry(self)
        self.testentry.insert("end", "This should be working")
        self.testentry.grid(row=1,column=1)

        self.testbutton = tk.Button(self, text="Test the label", command=self.updatelabel)
        self.testbutton.grid(row=1,column=2)

    def updatelabel(self):
        FrameOne.LabelText.set(self.testentry.get())   #HOW TO FIX THIS CODE THE RIGHT WAY?


#Create a window as defined in the AppWindow class
mainWindow = AppWindow(tk.Tk()) 

#Create a Frame as defined in class FrameOne
FirstFrame = FrameOne(mainWindow)
FirstFrame.grid(row=0, column=0) #Positioning Frame on Window

#Create a Frame as defined in class FrameOne
SecondFrame = FrameTwo(mainWindow)
SecondFrame.grid(row=1, column=0) #Positioning Frame on Window

Like with any python object, you access an attribute of an object using a reference to the object.与任何 python object 一样,您可以使用对 ZA8CFDE6331BD59EB2AC96F8911C46 的引用访问 object 的属性。

In your case, updatelabel should look like this:在您的情况下, updatelabel应如下所示:

def updatelabel(self):
    FirstFrame.LabelText.set(self.testentry.get()) 

Note: your use of uppercase characters for instance variables makes your code much harder to comprehend.注意:您对实例变量使用大写字符会使您的代码更难理解。 I recommend following the naming guidelines in PEP8 .我建议遵循PEP8中的命名指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM