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
  Using Tkinter inside function not working Ensaimadeta 5 4,859 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 786 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  Tkinter won't run my simple function AthertonH 6 3,742 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,736 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,416 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,918 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter get function finndude 2 2,890 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,230 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 4,964 Nov-24-2020, 11:28 PM
Last Post: Dale22
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 4,518 Oct-23-2020, 05:46 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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