Python Forum
[Tkinter] Accessing Entry with get Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Accessing Entry with get Function
#1
Hello,
New to Python. Below is my code for a multipage GUI using tkinter. I have classes set up for my Frames and more through them with Buttons. On the PreDataPage I have a couple Entry widgets. I am trying to use the save button to update the Dictionary preData{}. I believe this is something to do with the scope of the class....when I have the PreDataPage code and printNewEntry() in a script by themselves I have no issues using the get method on the Entry widget.

Any help would be appreciated. Thank you for any help you can provide.


from tkinter import *
#from test_preDataModule import *

LARGE_FONT = ("Verdana", 12)
NORM_FONT = ("Verdana", 10)
SMALL_FONT = ("Verdana", 8)


#Dictionary that will hold all the variables for pre-data.txt
preData = {'LEType':1,
           'LEa1': 641.92,
           'LEb1': 194.02,
           'LEx1': 340,
           'LEx2': 490,
           'LExm': 527,
           'LEc01': 28,
           'LEex1': 2.8,
           'LEc02': 12.0,
           'LEex2': 4.0,
           'TEType': 1,
           'TEa1': 643.28,
           'TEb1': 140.5,
           'TEx1': 180,
           'TExm': 527,
           'TEc0': -8.9,
           'TEy0': 88.06,
           'TEexp': 1.5,
           'VAType': 2,
           'VA1a': 741.33,
           'VA1b': 10.13,
           'VA2a': 372,
           'VA2b': 12.72,
           'VA3a': 288.41,
           'VA3b': 24.74,
           'VA4a': 112.185,
           'VA4b': 37.41,
           'CellWidth': 3,
           'Proportion': 0.2,
           'TotalCellNumber': 33}

spacer = '**********************************'
titleCellDistribution = '* 4. Cells distribution'



#================================================================
# Define CLASSES
#================================================================

class leParaglidingApp(Tk):

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        Tk.wm_title(self, "LeParagliding GUI")
        container = Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        #This the start of our MenuBar
        menubar = Menu(container)
        #Starting DROP DOWN for FILE
        listFile = Menu(menubar, tearoff=0)
        listFile.add_command(label="New Project", command = lambda: pop_Up_Msg("File - New Project"))
        listFile.add_command(label="Open Project", command = lambda: pop_Up_Msg("File - Open Project"))
        listFile.add_command(label="Save Project", command = lambda: pop_Up_Msg("File - Save Project"))
        listFile.add_separator()
        listFile.add_command(label="Exit", command=quit)
        #Starting DROP DOWN for EDIT
        listPreData = Menu(menubar, tearoff = False)
        listPreData.add_command(label='Edit', command = lambda:pop_Up_Msg('New PreData'))
        listPreData.add_command(label='New PreData', command = lambda:pop_Up_Msg('New PreData'))
        listPreData.add_command(label='Load PreData', command = lambda:pop_Up_Msg('Load PreData'))
        #Starting DROP DOWN for MAIN
        listLayout = Menu(menubar,tearoff = False)
        listLayout.add_command(label='Edit', command = lambda:pop_Up_Msg('Pre Data'))

        #below will show up across the Menu Bar, ALWAYS VISIBLE
        menubar.add_cascade(label='File', menu = listFile)
        menubar.add_cascade(label='PreData', menu = listPreData)
        menubar.add_cascade(label='Layout', menu = listLayout)
        
        Tk.config(self, menu = menubar)

        self.frames = {}

        for F in (StartPage, PreDataPage, LayoutPage):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column = 0, sticky = 'nsew')

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

    

class StartPage(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="Start Page", font=27)
        label.pack(pady=10, padx=10)
        label1 = Label(self, text='''
            Welcome to LeParagliding GUI 2019. Our goal is the make the LeParagliding App
            more accessable. We have tried to simplify the process and skills needed to run
            this program. For any bugs or improvements contact us at
            [email protected]

                                    ''', font = LARGE_FONT)
        label1.pack()
        button = Button(self, text="Visit PreData",
                            command=lambda: controller.show_frame(PreDataPage))
        button.pack()

        button2 = Button(self, text="Edit Layout",
                            command=lambda: controller.show_frame(LayoutPage))
        button2.pack()

class PreDataPage(Frame):
    #MODULE FRAMES_preData_Tab import *
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        root = Frame(self)
        root.pack(side="top", fill="both", expand = True)
        root.grid_rowconfigure(0, weight=1)
        root.grid_columnconfigure(0, weight=1)

        #Creation of all the main containers
        entryManager = Frame(root, bg='red')
        viewManager = Frame(root, bg='cyan')

        #layout of main container
        root.grid_rowconfigure(0, weight=1)
        root.grid_columnconfigure(0, weight=1)
        root.grid_columnconfigure(1, weight=1)

        entryManager.grid(row=0,column=0, sticky='nsew')
        viewManager.grid(row=0, column=1, sticky='nsew')
        #Creation of all the main containers
        planformView = Frame(viewManager, bg='white')
        vaultView = Frame(viewManager, bg='yellow')

        viewManager.grid_rowconfigure(0, weight=1)
        viewManager.grid_rowconfigure(1, weight=1)
        viewManager.grid_columnconfigure(0, weight=1)

        planformView.grid(row=0, column=0, sticky='nsew')
        vaultView.grid(row=1, column=0, sticky='nsew')

        b1 = Button(planformView, text='Get New Entry', command = lambda:printNewEntry())
        b1.pack()


        wingNameEntry = Frame(entryManager, bg='purple')
        LeadingEdgeEntry = Frame(entryManager, bg='black')
        TrailingEdgeEntry = Frame(entryManager, bg='green')
        VaultEntry = Frame(entryManager, bg='blue')
        CellDistributionEntry = Frame(entryManager, bg='orange')

        entryManager.grid_rowconfigure(0, weight=1)
        entryManager.grid_rowconfigure(1, weight=1)
        entryManager.grid_rowconfigure(2, weight=1)
        entryManager.grid_rowconfigure(3, weight=1)
        entryManager.grid_rowconfigure(4, weight=1)
        entryManager.grid_columnconfigure(0, weight=1)

        wingNameEntry.grid(row=0, column=0, sticky='nsew')
        LeadingEdgeEntry.grid(row=1, column=0, sticky='nsew')
        TrailingEdgeEntry.grid(row=2, column=0, sticky='nsew')
        VaultEntry.grid(row=3, column=0, sticky='nsew')
        CellDistributionEntry.grid(row=4, column=0, sticky='nsew')



        CellDistributionEntry.grid_rowconfigure(0, weight=1)
        CellDistributionEntry.grid_rowconfigure(1, weight=1)
        CellDistributionEntry.grid_rowconfigure(2, weight=1)
        CellDistributionEntry.grid_rowconfigure(3, weight=1)
        CellDistributionEntry.grid_columnconfigure(0, weight=1)
        CellDistributionEntry.grid_columnconfigure(1, weight=1)

        CDvar1 = StringVar()
        CDvar2 = StringVar()
        CDvar3 = StringVar()

        CDlab = Label(CellDistributionEntry, text='Cell Distribution Parameters')
        CDlab1 = Label(CellDistributionEntry, text='Cell Width: ')
        CDent1 = Entry(CellDistributionEntry, textvariable=CDvar1)
        CDlab2 = Label(CellDistributionEntry, text='Proportion: ')
        CDent2 = Entry(CellDistributionEntry, textvariable=CDvar2)
        CDlab3 = Label(CellDistributionEntry, text='Total Cell Number: ')
        CDent3 = Entry(CellDistributionEntry, textvariable=CDvar3)

        CDvar1.set(str(preData['CellWidth']))
        CDvar2.set(str(preData['Proportion']))
        CDvar3.set(str(preData['TotalCellNumber']))

        CDlab.grid(row=0, column=0, columnspan=4, sticky='nsew')

        CDlab1.grid(row=1, column=0, sticky='nsew')
        CDent1.grid(row=1, column=1, sticky='nsew')
        CDlab2.grid(row=2, column=0, sticky='nsew')
        CDent2.grid(row=2, column=1, sticky='nsew')
        CDlab3.grid(row=3, column=0, sticky='nsew')
        CDent3.grid(row=3, column=1, sticky='nsew')
        
class LayoutPage(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="Edit Layout", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = Button(self, text="Edit PreData",
                            command=lambda: controller.show_frame(PreDataPage))
        button2.pack()

#================================================================
# Define FUNCTIONS
#================================================================
def printNewEntry():
    '''
        Will use get function on entry to update list and then write data to file
    '''
    print('We are in printNewEntry')
    preData['CellWidth'] = CDvar1.get()
    preData['Proportion'] = CDvar2.get()
    preData['TotalCellNumber'] = CDvar3.get()

    print(preData['CellWidth'])
    print(preData['Proportion'])
    print(preData['TotalCellNumber'])

def pop_Up_Msg(msg):
    popup = Tk()
    popup.wm_title("!")
    label = Label(popup, text=msg, font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    B1 = Button(popup, text="Okay", command = popup.destroy)
    B1.pack()
    popup.mainloop()
    
def new_Project():
    '''
    Create New Folder (Using YYYY_DD_MM_HH_MM) will be MAIN FOLDER
    Create Sub Folder Pre Processor 'pre1.5' - NEED FOR MAKE CODE TO PULL VERSION FROM FILE PATH
    Create Sub Folder Main Program 'lep'
    Place aerofoils in 'lep' folder
    Pull Programs from STORAGE and put in new folders
    '''
    #try to clear gui
    label = Label(myGui, text = 'Creating New Project').pack()
    #move to preData editing tab

def load_Project():
    file1 = filedialog.askopenfile()
    label = Label(myGui, text = file1).pack()
    '''
        This gets us the filepath for project to load
        need to then load this into the variables for the open file
    '''

#================================================================
# START PROGRAM
#================================================================

app = leParaglidingApp()
app.geometry("1280x720")
app.mainloop()
Reply
#2
Hi nexgenskydiver

Some possible modifications:
        # Modified in (class PreDataPage)
        b1 = Button(planformView, text='Get New Entry', command = lambda:printNewEntry(*CDvars))
        b1.pack()

        CDvar1 = StringVar()
        CDvar2 = StringVar()
        CDvar3 = StringVar()
        
        # New in (class PreDataPage)
        CDvars = (CDvar1, CDvar2, CDvar3)
and:

# Modified function (printNewEntry)
def printNewEntry(*args):
    '''
        Will use get function on entry to update list and then write data to file
    '''
    print('We are in printNewEntry', args)
    CDvar1, CDvar2, CDvar3 = args
    preData['CellWidth'] = CDvar1.get()
    preData['Proportion'] = CDvar2.get()
    preData['TotalCellNumber'] = CDvar3.get()
 
    print(preData['CellWidth'])
    print(preData['Proportion'])
    print(preData['TotalCellNumber'])
wuf Wink
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,420 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,453 Mar-18-2019, 05:36 PM
Last Post: woooee
  how to bind entry to a function in another file Rishav 1 2,953 Aug-07-2017, 04:34 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020