如何从另一个文件的类中正确访问变量?

时间:2019-04-06 21:18:16

标签: python tkinter

我目前正在尝试使用tkinter为将来的项目制作UI。但是,在设置一些基本的OOP概念时遇到了很多麻烦。

我有两个文件main.pypressed.py,我正在尝试为pressed.py中的按钮按下编写函数,但是我不确定该怎么做可以访问main.py

中的变量

这是我当前遇到的错误:

Class 'UI' has no 'py_entry' member 

我已经尝试使用许多其他Stack Overflow帖子作为参考,但是没有一个起作用。

main.py

import tkinter as tk
import os
import geocoder

from tkinter import messagebox
from PIL import Image
from PIL import ImageTk

import pressed

# Paths
assets_path = "./assets/"

class UI(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        # Set window title
        parent.title("geo-location-data")

        # Set window geometry
        parent.geometry("800x600")

        # Address entry label
        py_label = tk.Label(parent, text="Emter an addres:", font=("Helvetica", 12))
        py_label.place(x=0, y=0)

        # Address entry field
        py_entry = tk.Entry(parent, font=("Helvetica", 12))
        py_entry.place(x=130, y=2)

        # Open button_normal.png
        button_normal_img = Image.open(assets_path + "button_normal.png")

        # Resize button_normal.png
        button_normal_img_resize = button_normal_img.resize((16, 16), Image.BILINEAR)
        button_normal_img_resize_format = "resized_button_normal" + ".png"

        # Set path
        path_for_button_normal = os.path.join(assets_path, button_normal_img_resize_format)

        # Save to path
        button_normal_img_resize.save(path_for_button_normal)

        # Open saved image
        button_normal_img_r_open = Image.open(assets_path + "resized_button_normal.png")
        button_normal_img_r_open = ImageTk.PhotoImage(button_normal_img_r_open)

        #def button_pressed(self):
            # If address entry field is blank
            #if(py_entry.index("end") == 0):
                #messagebox.showerror("Error", "Entry field was left blank.")
                #return self
            #else:
                #print("test")
                #return self

        # Pressed

        # ADD COMMAND #
        py_button = tk.Button(parent, font=("Helvetica", 12), width=16, height=16, image=button_normal_img_r_open)
        py_button.place(x=320, y=2)

if __name__ == "__main__":
    root = tk.Tk()
    UI(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

pressed.py

from sample import main

from tkinter import messagebox

class Pressed(main.UI):
    def __init__(self):
        def button_press(self):
            # This is where I'm getting the error
            if (main.UI.):
                messagebox.showerror("Error", "Address entry field was left blank.")
                return self
            else:
                print("Button pressed!")
                return self

1 个答案:

答案 0 :(得分:0)

您的问题似乎是您在类内部声明了只能从__init__()函数内部访问的局部变量。 Python有一种创建类变量的方法,方法是在类内部(方法之外)声明它们,或调用self.variable_name。然后,您可以通过在类中的每个位置调用self.variable_name来访问该变量。
self是对您课程的当前实例的引用。

在您的示例中,您可能想要声明变量的方式是使用self关键字:

class UI(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.py_entry = tk.Entry(parent, font=("Helvetica", 12))
        self.py_entry.place(x=130, y=2)

在创建py_entry类的实例(调用UI)之后,您应该能够访问UI()字段。