简体   繁体   English

无法获得 Tkinter class 之外的值

[英]Not able to get Tkinter Values outside of class

I have created a simple tKinter Gui with PAGE builder and I am able to click a button and execute the corresponding command function within it.我用 PAGE 构建器创建了一个简单的 tKinter Gui,我可以单击一个按钮并在其中执行相应的命令 function。 But when I try to get a value of a specific text box within the function I get various errors mostly no such property found.但是,当我尝试获取 function 中特定文本框的值时,我遇到了各种错误,大部分是找不到此类属性。 I have tried adding self and the class name into the property and even passing the property from the class as well as making it a function within that class but I still can't seem to access the values of the textbox 'Username'.我尝试将 self 和 class 名称添加到属性中,甚至从 class 传递属性,并在 class 中使其成为 function,但我似乎仍然无法访问文本框“用户名”的值。 I would really appreciate any help on how to get those text box values within the function as I have been researching for hours but still cannot make it work.我真的很感激任何有关如何在 function 中获取这些文本框值的帮助,因为我已经研究了几个小时但仍然无法使其工作。 Also if anyone knows of any good tutorial on this topic would help tremendously.此外,如果有人知道关于此主题的任何好的教程,将会有很大帮助。 Thank you.谢谢你。

The project has 2 files: (I've tried to remove the non essential code)该项目有 2 个文件:(我试图删除非必要的代码)

MacUpdaterPageDesign.py MacUpdaterPageDesign.py

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import os.path

_script = sys.argv[0]
_location = os.path.dirname(_script)

import MacUpdaterPageDesign_support

class Toplevel1:
    def __init__(self, top=None):
        
        top.title("Mac Updater")
        top.configure(background="#d9d9d9")

        self.top = top
        
        self.MainFrame = tk.Frame(self.top)
        self.MainFrame.place(relx=0.0, rely=0.18, relheight=0.811
                , relwidth=1.099)

        self.Username = tk.Text(self.MainFrame)
        self.Username.place(relx=0.15, rely=0.081, relheight=0.048
                , relwidth=0.279)
        
        #this button calls the CopyMACfunc on the support page      
        self.CopyMAC = tk.Button(self.MainFrame)
        self.CopyMAC.place(relx=0.143, rely=0.846, height=34, width=117)
        self.CopyMAC.configure(command=MacUpdaterPageDesign_support.CopyMACfunc)
        self.CopyMAC.configure(text='Copy MAC')

MacUpdaterPageDesign_support.py MacUpdaterPageDesign_support.py

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import MacUpdaterPageDesign

def main(*args):
    '''Main entry point for the application.'''
    global root
    root = tk.Tk()
    root.protocol( 'WM_DELETE_WINDOW' , root.destroy)
    # Creates a toplevel widget.
    global _top1, _w1
    _top1 = root
    _w1 = MacUpdaterPageDesign.Toplevel1(_top1)
    root.mainloop()

def CopyMACfunc(*args):
    #this part must retrieve the value in from Username
    #tried many variations of below but throws error
    username = MacUpdaterPageDesign.Username.get("1.0",END)
    print(username) 


if __name__ == '__main__':
    MacUpdaterPageDesign.start_up()

Actually while writing this question I finally got it to work:实际上,在写这个问题时,我终于让它起作用了:

username = _w1.Username.get("1.0",END)

it did work with the following but not sure if this would be the right way to do it per say.它确实适用于以下但不确定这是否是正确的方法。 Maybe their are better ways if anyone knows.如果有人知道,也许他们是更好的方法。 Also would appreciate any recommendation for a good tutorial or where to learn all of this type of info.也将不胜感激任何有关好的教程或在哪里可以学习所有此类信息的建议。 Thanks谢谢

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

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