【问题标题】:How to get a variable from a parameter function outside his class?如何从他的类之外的参数函数中获取变量?
【发布时间】:2019-10-03 10:17:04
【问题描述】:

我正在使用 tkinter 库在 python 中为我自己的项目创建一个 GUI 界面。

在一个框架上,我设置了一个 tk.Entry 让我写一些文本。 我想在下一帧中使用此信息(假设在 tk.Label 中简单) 但我无法获得信息,似乎是因为这两个函数属于不同的类。

试图将 private_key 设为全局,但似乎覆盖了定义。 试图返回 private_key 但我仍然无法访问它,因为我无法在下一个类中调用参数。 尝试在下一节课中再次使用该功能,同样的问题。 尝试在 PVK 类中设置标签,似乎也不起作用

from tkinter import *

# type and size of font
LARGE_FONT = ('MS Serif', 15)
# white writing color
FRONT_COLOR = '#ffffff'
# dark_gray background color
BACKGROUND_COLOR = '#272727'


class Bobby(Tk):

    # Used each time the function is called
    def __init__(self):
        # init tkinter
        Tk.__init__(self)

        Tk.iconbitmap(self, default='bobby.ico')
        Tk.wm_title(self, "Bobby")
        Tk.geometry(self, '500x200')

        container = Frame(self)
        container.pack(side='top', fill='both', expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for f in (PVK, Display):

            frame = f(container, self)

            self.frames[f] = frame

            frame.grid(row=0, column=0, sticky=NSEW)

        self.show_frame(Welcome)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class PVK(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.configure(background=BACKGROUND_COLOR)
        here = Label(self, text="here", font=LARGE_FONT, background=BACKGROUND_COLOR, fg=FRONT_COLOR)
        here.grid()
        self.pvk = Entry(self, show=" ")
        self.pvk.bind('<Return>', self.check)
        self.pvk.grid()

    def check(self, event):
        private_key = int(self.pvk.get()), 11413
        bobby.show_frame(Display)
        return private_key

class Display(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.configure(background=BACKGROUND_COLOR)

        #want to display it here
        Label(self, text=str(PVK.private_key)).grid()


bobby = Bobby()
bobby.mainloop()

我希望显示带有文本的标签,这意味着我可以使用该变量。 我目前收到 private_key 未定义的错误。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    由于您将controller 传递到Display__init__(),那么您可以找到PVK 类的实例:

    class PVK(Frame):
    
        def __init__(self, parent, controller):
            # stuff omitted
    
        def check(self, event):
            self.private_key = int(self.pvk.get()), 11413  # note self.private_key
            bobby.show_frame(Display)
    
    class Display(Frame):
    
        def __init__(self, parent, controller):
            Frame.__init__(self, parent)
            self.configure(background=BACKGROUND_COLOR)
    
            #want to display it here
            private_key = controller.frames[PVK].private_key
            Label(self, text=str(private_key)).grid()
    

    还有其他方法可以访问private_key,例如PVK可以将其写回控制器:self.controller.private_key = private_key

    【讨论】:

    • 您的两个解决方案都对我犯了同样的错误:“return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'self'”调试告诉我他没有' t 喜欢“private_key = controller.self.frames[PVK].private_key”这一行。在第一种方式中,我还在“self.private_key = int(self.pvk.get()), 11413”处收到警告它说它检测到 init 方法之外的实例属性定义:/ 不确定是什么去做
    • 对不起,controller.self 是一个错误。应该是controller.frames[PVK].private_key
    • 要修复警告,您可以将self.private_key = None 添加到__init__() of PVK
    • 实际上需要您的两个答案。代码运行没有错误,但我没有得到我想要的结果。在类显示中的我的标签中,文本设置为“无”,正如我在 PVK 的__init__() 中初始化的那样。我还尝试在我的检查功能中添加一个返回,但没有帮助
    • 哦,我明白了。 Label 是在 __init__()Display 中创建的,以后不会更新。
    猜你喜欢
    • 2018-05-23
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2011-09-21
    相关资源
    最近更新 更多