Bottom Page

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()
    
Quote
#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)
jagasrik likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter get method is not accepting value jagasrik 4 93 Sep-15-2020, 05:41 PM
Last Post: jagasrik
  [Tkinter] Use function from other class (Tkinter) zarize 8 311 Aug-17-2020, 09:47 AM
Last Post: zarize
  [Tkinter] updating tkinter chart from within function mikisDW 1 208 Jul-02-2020, 03:33 AM
Last Post: deanhystad
  Tkinter - Issues with "iconbitmap" method aquerci 3 604 May-21-2020, 09:46 AM
Last Post: aquerci
  Passing arguments into function, tkinter nanok66 3 489 Apr-18-2020, 11:53 PM
Last Post: nanok66
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 385 Apr-17-2020, 08:12 PM
Last Post: johnjh
  Tkinter:Unable to bind and unbind function with a button shallanq 2 504 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  Multiply function using Tkinter ady1583 5 2,237 Dec-21-2018, 02:59 PM
Last Post: buran
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 12,308 Aug-05-2018, 11:01 PM
Last Post: woooee
  Python, Tkinter, can only view partial GUI when called from another window Philia 1 1,248 Jul-17-2018, 06:38 PM
Last Post: woooee

Forum Jump:


Users browsing this thread: 1 Guest(s)