Getting an entry widget's content into a separate function

102 views Asked by At

For very personal reasons, I program procedurally. Let's say I create a Tkinter window with an entry widget and a button widget in one function and the button triggers another function which is supposed to process the entry widget's content.

So far I have only managed to get it working with StringVar and global variables.

Can I avoid classes AND global variables?

#!/usr/bin/python

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

import os


def output():
    global gamename
    print("The widget's value is: " + gamename.get())

def newGame():
    global gamename

    win1 = tk.Toplevel()   

    e = tk.Entry(win1, textvariable = gamename)
    e.place(x = 0, y = 30, width=200, height=30)     
    outp = tk.Button(win1, text="Print", command=output)
    outp.place(x = 0, y = 110, width=200, height=30)

    win1.mainloop()

root = tk.Tk()
gamename = tk.StringVar()

newGame()
tk.mainloop()
1

There are 1 answers

0
Bryan Oakley On BEST ANSWER

"Can I avoid classes AND global variables?"

No.

There's simply no good reason to not use classes when writing large programs in python. If you're writing small programs, there's nothing wrong with using globals.