Python Forum
[Tkinter] Selected radio button in push button in Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Selected radio button in push button in Tkinter
#1
my question here

my code here
Hello everyone i am learning about Python GUI stuff,

I have to use 4 radio buttons and a push button.
I created following code for the same.
import Tkinter as tk
from Tkinter import *
import ttk

root = tk.Tk()
root.title("GUI")
root.geometry("300x200")


#####################___________ Profile-1 ___________#####################    
def Profile_1():
   print('Profile 1 selected')

#####################___________ Profile-2 ___________#####################  
def Profile_2():
   print('Profile 2 selected')

#####################___________ Profile-3 ___________#####################  
def Profile_3():
   print('Profile 3 selected')
#####################___________ Profile-4 ___________#####################          
def Profile_4():
   print('profile 4 selected')


def serial_port():
   global var
   print('It comes in to push button menu')
   x = Profile_sel_1.selection_get()
   print(x)
   
var = IntVar()
#Button to show selected profile to assign FTW
Profile_sel_1=Radiobutton(root, text='Profile Selection_1',variable =var, value =1,command = Profile_1,width = 20)
Profile_sel_2=Radiobutton(root, text='Profile Selection_2',variable =var, value =2,command = Profile_2,width = 20)
Profile_sel_3=Radiobutton(root, text='Profile Selection_3',variable =var, value =3,command = Profile_3,width = 20)
Profile_sel_4=Radiobutton(root, text='Profile Selection_4',variable =var, value =4,command = Profile_4,width = 20)
#Button to show entered reg values and data in it
ser_port_btn=Button(root, text='ENTER',command = serial_port,relief="flat",background="#ccc",width=20)
Profile_sel_1.grid(row=1,column = 5)
Profile_sel_2.grid(row=2,column = 5)
Profile_sel_3.grid(row=3,column = 5)
Profile_sel_4.grid(row=4,column = 5)
ser_port_btn.grid(row = 7, column = 3)

root.mainloop()
But When Profile_sel_1 is active high that time function inside it should be used after pressing push button.
Reply
#2
Hm, still no answer...
If I understood it right, you want to do an action after you've pressed the button. In this case you just remove command in Radiobutton. Use instead inside the function var.get() to get the current selection. To put everything in functions is better. The use of global is not good. Instead you can prepare a function call with lambda or partial from functools. Here your changed code.

# boiler plate code for python2/3 compatibility
# i don't like to run python2.7
from __future__ import print_function
from functools import partial
try:
    import Tkinter as tk
    from Tkinter import *
    import ttk
except ModuleNotFoundError:
    import tkinter as tk
    from tkinter import *  
    from tkinter import ttk
# you don't need it


def serial_port(var):
    selection = var.get()
    # get the value out of var, which has been defined in main()
    print('Pushed the button!')
    print('var has value', selection)
    # to save space
    text_dict = {
        0: 'Nothing selected!', 
        1: 'Profile 1 selected',
        2: 'Profile 2 selected',
        3: 'Profile 3 selected',
        4: 'profile 4 selected'
        }
    # you can also put functions as a value to the text_dict
    # and call them after you got the value
    # d = {1: function}
    # d[1]('do_something')
    # just as a hint
    text_to_print = text_dict[selection]
    print(text_to_print)


def main():
    root = tk.Tk()
    root.title("GUI")
    root.geometry("300x200")    
    var = IntVar()
    #Button to show selected profile to assign FTW
    profile_sel_1=Radiobutton(root, text='Profile Selection_1', variable=var, value=1, width=20)
    # just removed command
    # you can preselect a Radiobutton with:
    # profile_sel_1.select()
    # or you can use the variable var, to select the Radiobutton
    # var.set(1)
    profile_sel_2=Radiobutton(root, text='Profile Selection_2', variable=var, value=2, width=20)
    profile_sel_3=Radiobutton(root, text='Profile Selection_3', variable=var, value=3, width=20)
    profile_sel_4=Radiobutton(root, text='Profile Selection_4', variable=var, value=4, width=20)
    # Button to show entered reg values and data in it
    ser_port_btn=Button(root, text='ENTER', command=partial(serial_port, var), relief="flat", background="#ccc", width=20)
    # or use (..., command=lambda: serial_port(var), ...)
    profile_sel_1.grid(row=1, column=5)
    profile_sel_2.grid(row=2, column=5)
    profile_sel_3.grid(row=3, column=5)
    profile_sel_4.grid(row=4, column=5)
    ser_port_btn.grid(row=7, column=3)
    root.mainloop()


if __name__ == '__main__':
    main()
Later you want to use classes for it. It makes your code more modular and you can save the state inside the class.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question closing a "nested" window with a button in PySimpleGUI and repeating this process Robby_PY 6 334 Jan-10-2021, 10:34 PM
Last Post: Serafim
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 111 Dec-28-2020, 10:00 PM
Last Post: nanok66
  Button to clear all output labels? AnunnakiKungFu 5 295 Dec-18-2020, 10:12 AM
Last Post: backoboy10
  Get name of command button Heyjoe 3 197 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  Button to add data to database and listbox SalsaBeanDip 1 228 Dec-06-2020, 10:13 PM
Last Post: Larz60+
  How to disable custom button Sancho_Pansa 7 243 Dec-04-2020, 02:21 PM
Last Post: buran
  [Tkinter] showing return from button on main screen blazejwiecha 4 262 Nov-22-2020, 04:33 PM
Last Post: blazejwiecha
  Made new instance - button still goes to old instance nanok66 6 403 Nov-06-2020, 07:44 PM
Last Post: deanhystad
  [Tkinter] Button click problem using OOP JohnB 5 343 Oct-21-2020, 12:43 PM
Last Post: JohnB
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 670 Oct-18-2020, 10:58 AM
Last Post: TurboC

Forum Jump:

User Panel Messages

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