Python Forum
tkinter get method is not accepting value when called by function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter get method is not accepting value when called by function
#1
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()
    
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function in new window (tkinter) Dale22 7 209 Nov-24-2020, 11:28 PM
Last Post: Dale22
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 172 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 302 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  Tkinter menu font size -method to change tonycat 2 312 Oct-11-2020, 02:43 AM
Last Post: tonycat
  tkinter get method is not accepting value jagasrik 4 218 Sep-15-2020, 05:41 PM
Last Post: jagasrik
  [Tkinter] Use function from other class (Tkinter) zarize 8 508 Aug-17-2020, 09:47 AM
Last Post: zarize
  [Tkinter] updating tkinter chart from within function mikisDW 1 305 Jul-02-2020, 03:33 AM
Last Post: deanhystad
  Tkinter - Issues with "iconbitmap" method aquerci 3 994 May-21-2020, 09:46 AM
Last Post: aquerci
  Passing arguments into function, tkinter nanok66 3 640 Apr-18-2020, 11:53 PM
Last Post: nanok66
  Tkinter:Unable to bind and unbind function with a button shallanq 2 747 Mar-28-2020, 02:05 AM
Last Post: joe_momma

Forum Jump:

User Panel Messages

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