简体   繁体   中英

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. 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. 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'. 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. 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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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