Python Forum
tkinter get method is not accepting value when called by function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: GUI (https://python-forum.io/Forum-GUI)
+--- Thread: tkinter get method is not accepting value when called by function (/Thread-tkinter-get-method-is-not-accepting-value-when-called-by-function)



tkinter get method is not accepting value when called by function - jagasrik - Sep-16-2020

I want to call print value in a after getting the input from user, function myoptions() just take the input from the user and calls user_choice to print the input taken, if i dont use myoptions and retain the myoptions code it works perfectly but when i use it (myoptions) to call a function user_choice is giving me option not defined or null, please let me know whats wrong when calling by function. Please do explain me whats happening.


This works

import tkinter as tk

def user_choice():
    a = (option.get())
    print(a)

    
   
root1 = tk.Tk() 
option = tk.StringVar()  
canvas = tk.Canvas(root1,height=500,width=500)
canvas.pack()
frame = tk.Frame(root1,bg='#80c1ff')
frame.place(relx=0.1,rely=0.1,relwidth=0.8,relheight=0.8)
label = tk.Label(frame,
                 text="|| Enter number 1-4 || \n Please Choose your options",bg="yellow")
label.place(relx=0.15,rely=0.1,relwidth=0.7)
label = tk.Label(frame,
                 text="\n 1- Check saving Balance \n 2- Deposit Funds \n 3- Withdraw Funds \n 4- Transfer Funds ",bg="yellow")
label.place(relx=0.15,rely=0.2,relwidth=0.7)
entry = tk.Entry(root1,bg="gray",textvariable=option)
entry.place(relx=0.45,rely=0.43,relwidth=0.09)
button = tk.Button(master=root1, text='OK',bg='purple',activebackground='pink',fg='white',command=user_choice)
button.place(relx=0.41,rely=0.489,relwidth=0.16) 
root1.mainloop()

This wont work but i want this to work

def user_choice():
    a = (option.get())
    print(a)

    
def myoptions():    
    root1 = tk.Tk() 
    option = tk.StringVar()  
    canvas = tk.Canvas(root1,height=500,width=500)
    canvas.pack()
    frame = tk.Frame(root1,bg='#80c1ff')
    frame.place(relx=0.1,rely=0.1,relwidth=0.8,relheight=0.8)
    label = tk.Label(frame,
                     text="|| Enter number 1-4 || \n Please Choose your options",bg="yellow")
    label.place(relx=0.15,rely=0.1,relwidth=0.7)
    label = tk.Label(frame,
                     text="\n 1- Check saving Balance \n 2- Deposit Funds \n 3- Withdraw Funds \n 4- Transfer Funds ",bg="yellow")
    label.place(relx=0.15,rely=0.2,relwidth=0.7)
    entry = tk.Entry(root1,bg="gray",textvariable=option)
    entry.place(relx=0.45,rely=0.43,relwidth=0.09)
    button = tk.Button(master=root1, text='OK',bg='purple',activebackground='pink',fg='white',command=user_choice)
    button.place(relx=0.41,rely=0.489,relwidth=0.16) 
    root1.mainloop()
    
myoptions()
    



RE: tkinter get method is not accepting value when called by function - Yoriz - Sep-16-2020

By moving option into the function myoptions it becomes a local variable that is not accessible to the function user_choice.
It is far easier to use classes with tkinter (no time right now to show an example)