tkinter 开发:为了将 GUI 界面和函数实现分开,定义了全局变量,使用全局变量时报错

在作tkinter开发时,为了将GUI界面的实现和具体的逻辑实现分离开,定义了一些全局变量,但全局变量使用时会报错,不知道什么原因,望各位路过的大侠指正,谢谢!
GlobalVar.py
init函数中:
savePathEntry=tk.Entry()
pullFrameSavePathEntryStr=tk.StringVar()
GUI界面实现时: GlobalVar.savePathEntry=tk.Entry(selectPathFrame,width=25,textvariable=GlobalVar.pullFrameSavePathEntryStr)
GlobalVar.savePathEntry.pack(side=tk.LEFT)
使用GlobalVar.savePathEntry没有任何问题,但使用GlobalVar.pullFrameSavePathEntryStr报错:AttributeError: module ‘GlobalVar’ has no attribute ‘pullFrameSavePathEntryStr’
谢谢!

Jason990420
最佳答案

For example, I have one Setting.py for GUI definition, and another main script Graphic. They are in same working directory.

# Setting.py

from tkinter import Tk, Button

class GUI():

    def __init__(self):
        self.root = Tk()
        self.width = 640
        self.height = 480
        self.font = ('Courier New', 16)
        self.elements = {}
        self.root.bind('<Key-Escape>', self.quit)
        self.set_size()

    def set_size(self):
        self.root.geometry(f"{self.width}x{self.height}")

    def add_button(self, text, func):
        self.elements[text] = Button(
            self.root, text=text, font=self.font, command=func)
        self.elements[text].pack()

    def quit(self, event):
        self.root.destroy()
        quit()

    def mainloop(self):
        self.root.mainloop()
# Graphic.py
from Setting import GUI

def set_new_window_size():
    g.width, g.height = 320, 240         # Set width and height of GUI
    g.set_size()

def set_old_window_size():
    g.width, g.height = width, height    # Set width and height of GUI
    g.set_size()

g = GUI()

width, height = g.width, g.height        # Get width and height of GUI.
g.add_button('Button 1', set_new_window_size)
g.add_button('Button 2', set_old_window_size)

g.mainloop()
3年前 评论
讨论数量: 8

@Jason990420 谢谢关注
不好意思,补全代码:
只能以图片的形式发布代码了,直接发布代码的话会打乱排版,

file

3年前 评论
Jason990420

只要在 ``` python 前多空一行就不會打乱排版

3年前 评论
Jason990420

从以下几行, 可以看出来你对类中的变量定义理解有问题

class GlobalVar():
    def __init__(self):`
        savePathEntry = tk.Entry()
        pullFrameSavePath = tk.StringVar()

GlobalVar.savePathEntry = tk.Entry(...)

GlobalVar.pullFrameSavePat.set(selectPath)
  1. 在 __init__ 中的 savePathEntrypullFrameSavePath 都是临时变量, 不是类 GlobalVar 的属性, 也不是 GlobalVar 的实例属性, 以下定义为类实例的属性.
class GlobalVar():
    def __init__(self):`
        self.savePathEntry=tk.Entry()
        self.pullFrameSavePath=tk.StringVar()
  1. 以下代码是为 GlobalVar 建立一个新的属性或修改属性 savePathEntry, 你没有定义, 结果就是建立一个类的新属性, 当然没出错
GlobalVar.savePathEntry = tk.Entry(...)
  1. 以下代码调用 GlobalVar 的属性 pullFrameSavePat, 你没有定义, 当然就出错了.
GlobalVar.pullFrameSavePat.set(selectPath)
  1. 类属性与实例属性不同, 以下代码中, 类属性 GlobalVar.savePathEntry 为 1, 不因实例化而改变; 而实例属性 g.savePathEntry 为 2, 每个实例的属性可以不同. 两者所代表的是完全不同的变量.
class GlobalVar():
    savePathEntry = 1

    def __init__(self):
        self.savePathEntry = 2

g = GlobalVar()
3年前 评论

哦,谢谢答复,我再修改一下

3年前 评论

我大概明白了你的意思,但是还是有一个问题:我需要一个变量,在GUI实现文件中设置此变量值,然后在逻辑实现文件里面引用此值,或者换过来,在逻辑实现文件中设置此值,在GUI实现文件中设置此变量值,那我该怎么实现呢?

3年前 评论

我大概明白了你的意思,但是还是有一个问题:我需要一个变量,在 GUI 实现文件中设置此变量值,然后在逻辑实现文件里面引用此值,或者换过来,在逻辑实现文件中设置此值,在 GUI 实现文件中引用变量值,那我该怎么实现呢?

3年前 评论
Jason990420

For example, I have one Setting.py for GUI definition, and another main script Graphic. They are in same working directory.

# Setting.py

from tkinter import Tk, Button

class GUI():

    def __init__(self):
        self.root = Tk()
        self.width = 640
        self.height = 480
        self.font = ('Courier New', 16)
        self.elements = {}
        self.root.bind('<Key-Escape>', self.quit)
        self.set_size()

    def set_size(self):
        self.root.geometry(f"{self.width}x{self.height}")

    def add_button(self, text, func):
        self.elements[text] = Button(
            self.root, text=text, font=self.font, command=func)
        self.elements[text].pack()

    def quit(self, event):
        self.root.destroy()
        quit()

    def mainloop(self):
        self.root.mainloop()
# Graphic.py
from Setting import GUI

def set_new_window_size():
    g.width, g.height = 320, 240         # Set width and height of GUI
    g.set_size()

def set_old_window_size():
    g.width, g.height = width, height    # Set width and height of GUI
    g.set_size()

g = GUI()

width, height = g.width, g.height        # Get width and height of GUI.
g.add_button('Button 1', set_new_window_size)
g.add_button('Button 2', set_old_window_size)

g.mainloop()
3年前 评论

@Jason990420 谢谢哈,问题解决了

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!