Radio Buttons with TKinter - Python Tkinter GUI Tutorial #12
How to use Radio Buttons with TKinter and Python. In this video I'll show you how to use Radio Buttons with your python tkinter program.
In this series I'll show you how to create graphical user interfaces for Python with Tkinter.
✅ Watch The Other Videos In This Python Playlist:
bit.ly/2UFLKgj
▶️ See More At:
Codemy.com
✅ Join My Facebook Group:
bit.ly/2GFmOBz
✅ Subscribe To My KZread Channel:
bit.ly/2IGzvOR
▶️ Learn to Code at Codemy.com
Take $22 off with coupon code: youtube
Пікірлер: 164
▶️ Watch Entire Tkinter Playlist ✅ Subscribe To My KZread Channel: bit.ly/2UFLKgj bit.ly/2IGzvOR ▶️ See More At: ✅ Join My Facebook Group: Codemy.com bit.ly/2GFmOBz ▶️ Learn to Code at Codemy.com ✅ Buy a Codemy T-Shirt! Take $30 off with coupon code: youtube1 bit.ly/2VC9WUN
It's a teacher's day in India today. Happy teacher's day John! You have helped me here a lot. Great work.
@Codemycom
3 жыл бұрын
Thanks!
I love these videos. I refer back to these everyday to try and build my stuff
@Codemycom
3 жыл бұрын
Glad you find them useful!
U make me love Python more and more ! Tkx for ur effort !
Your videos are amazing...exactly what i want print("Thank you")
@Codemycom
3 жыл бұрын
Glad you like them!
Cok tesekkur ederim,emegine yureginize saglik.
Thank you, I am learning tkinter right now and I wanted to let you know I am having trouble learning some of it, and you are good at explaining.
@Codemycom
Жыл бұрын
Happy to hear it!
awesome, thanks!
you are amazing sir print('THANKS')
@Codemycom
4 жыл бұрын
Thanks!!
Thanks! Finally I understand that logic!
@Codemycom
Жыл бұрын
Happy to hear it!
Hi, thank you very much for your video.
@Codemycom
3 жыл бұрын
Thank you too
Thanks for sharing Sir
@Codemycom
Жыл бұрын
Welcome
Amazing waiting
@Codemycom
2 жыл бұрын
waiting for what?
Superliked, Thanks.
@Codemycom
2 жыл бұрын
Glad you liked it!
if name==“John”: print(“You are awesome and a great teacher!”)
@Codemycom
3 жыл бұрын
print("Thanks!")
Nice video sir....
@Codemycom
4 жыл бұрын
Thanks!
you just awsome sir make me code a lot better :-}
@Codemycom
4 жыл бұрын
Thanks!
very good
@Codemycom
2 жыл бұрын
Thanks!
good
I used grid to update the labels containing string, every time label gets updated there are some fragments of text of previous label seen beneath the current one (for ecample if first label was Pepperoni then next was egg), so "P" and "i" of Pepperoni will still be visible when egg is the new updated label.
@Codemycom
4 жыл бұрын
look into the .destroy() function.
Print("thank u")
@Codemycom
3 жыл бұрын
Sure thing
how can we make the buttons deselect themselves if we click on them again? like I tried making a multi correct MCQ, I assigned different variables to different buttons so one can select multiple buttons but how to deselect if we click on it again??
@Codemycom
4 жыл бұрын
Like I said, create a function that gets called every time the button is clicked that keeps track of the current state of the button and acts accordingly to deselect itself.
In this case it would be simpler to create a single label, with the attribute textvariable=pizza. Then it will always show which option is selected
@Codemycom
3 жыл бұрын
lot's of ways to do it
@ttd972
3 жыл бұрын
@@Codemycom yep, thanks for the great content!
Is possible to add multiple choices
So if I click a radio button it will consider one time entering in function or continuously as long as it is activated?
@durgeshchandrapatel5943
3 жыл бұрын
Only at the time when it is toggled like on or off.
is there a difference between root.mainloop() and just calling mainloop() ? i noticed that you used `mainloop()` instead of `root.mainloop()` starting w/ this vid (and onwards). also, do you have a tutorial on implementing tkinter in classes? (oop). thanks.
@Codemycom
4 жыл бұрын
Videos with classes will be coming soon :-)
hi, how can I pass the value to variable when "Click Me" button is pressed and close checkbox window automatically?
@Codemycom
4 жыл бұрын
I explain all of that in the videos in this playlist...
If i click a button, how do i make the screen of the gui clear of ? Can u plz help ?
@infinitespace1982
3 жыл бұрын
Maybe, you could try opening another window with the same name, and delete the original?
How can I get the text from the selected radiobutton and put it inside a normal variable??
@Codemycom
4 жыл бұрын
Don't we discuss that? If not, check the playlist.
sir? can we display the options and not have the value that we set be displayed on the screen to the user?
@Codemycom
4 жыл бұрын
Can't you tell from the video?
@akilamp8470
3 жыл бұрын
@@Codemycom I managed to figure it out. Thank you! Also can you tell me how to pass the value you get when you click a radiobutton in a seperate variable?
Sir can u tell me how to remove the current label nd whenever i click on any other radiobutton the label gets overwrite rather than showing its mode in next row?
@mdzohaib7368
4 жыл бұрын
I think there must be some command like label.rem smthng to remove the current label and replace in same row with other label whenever i click on other radiobutton.
@Codemycom
4 жыл бұрын
You can usually .config things to change them.
Please help. I am in a cybersecurity class and I have to make either a checkbutton or radio button and whenever I try it says there’s a Tcl error and means that Tcl wasn’t installed properly, but what is Tcl and how do I install it properly?
@Codemycom
Жыл бұрын
use the tools I use and the code I write and you won't have errors like that
How do you use the place() method with the radio button? The object is being created but not assigned to anything?
@Codemycom
4 жыл бұрын
I never use place, but you use it with the radio button the same way you use it with every other widget.
do u have any other tutorial in youtube ?
@Codemycom
4 жыл бұрын
Yes, I have hundreds: kzread.info
lambda looks like a capital L lol. got error on my code. good tutorial though.
@Codemycom
4 жыл бұрын
yeah, that drives me crazy...I don't know why sublime does that
So i did the same as you with the for loop but i had 7 radio buttons, i even set the value of the radio buttons variable to '1' but every time i run the program, all the buttons are marked but when i click on one button then all of the buttons get unmarked like it needs to be. how do i fix this problem?
@Codemycom
4 жыл бұрын
There's a problem with your code, but I don't know what.
@dead1ock284
4 жыл бұрын
@@Codemycom never mind i managed to figure it out but thanks anyways for replying to my comment :)
How can we bind radio buttons such that pressing certain keys selects those radio buttons, please make a video on this?
@Codemycom
3 жыл бұрын
I have videos in the playlist on bindings
@durgeshchandrapatel5943
3 жыл бұрын
@@Codemycom You don't have any of them to bind radio buttons.
@durgeshchandrapatel5943
3 жыл бұрын
@@Codemycom I want to navigate between options of a quiz, like if I click a , a gets selected...
i want ask you, why you use lambda in your command?
@Codemycom
3 жыл бұрын
Because that’s how it works.
Hi. I have stupid question. IntVar, Radiobutton, title, Label, .get on your editor have blue color. I'm using Pycharm, how to change colors for Tkinter commands? Thanks
@Codemycom
3 жыл бұрын
What could it possibly matter? Don't use pycharm
can any one please tell me how to disable the other radiobuttons once someone chooses onion?
hey, can we change color of dot in the radio button
@Codemycom
4 жыл бұрын
When defining the radio button, try setting the selectcolor="red" or whatever color
Great Video. I have one question though, When I tried to create radio buttons within a function, I can't seem to get the IntVar variable to change and it always stays as the default value, regardless of which radio button is selected. Is there a way to fix this?
@Codemycom
3 жыл бұрын
I think there must just be a typo in your code
@dong8912
3 жыл бұрын
@@Codemycom Nope, no typos. My script includes 6 radio buttons in total, 3 of which are outside a function and the other 3 in a function - the function is called by clicking a button, which opens up a new window that contains the other 3 radio buttons. All radio button lines look exactly the same, with only the values and texts being different. I even tried using global variables but that still didn't work. I think it may be helpful to mention that i am also using IDLE to run my scripts
@dong8912
3 жыл бұрын
So I managed to fix this issue, and also a bunch of other issues relating to some widgets not working in a function-generated window. Essentially, The very first window opened in the script uses the command: root = Tk(). since I'm creating more than one window concurrently, instead of using: window = Tk() to create that window, I instead use: window = Toplevel(). Apparently tkinter is designed such that only one Tk() window can be opened at a time, and to open multiple windows, I need to use Toplevel(). But yeah this fixes the issue of the radio buttons not working. Also I just realised you already made a video covering this and that I totally forgot.
@Codemycom
3 жыл бұрын
@@dong8912 Glad you got it sorted out!
@dong8912
3 жыл бұрын
@Codemy.com Thanks!
Can you create if statements using Radiobuttons
@Codemycom
4 жыл бұрын
Of course
Every time you click on the button, you get a new line whit the value of the radio button. If I want refresh the line and no insert a new line each time. How can I do?
@Codemycom
2 жыл бұрын
button_name.config(text=whatever)
I cant seem to get Lambda working is there a video where you explain it more?
@Codemycom
Жыл бұрын
you're mis-spelling lambda. It needs to have a lowercase l not L
How to unclick ? and when it's clicked print 1 else 0
my radiobutton does not deselect after choosing another one any solutions for the same??
@Codemycom
3 жыл бұрын
You just have a typo somewhere
strangely enough, text=r.get() doesnt work when using r as a StringVar(). Even after setting r.set="sample string"... Nothing shows up, when tried to check by print(r.get()), its a blank string.
@Codemycom
4 жыл бұрын
r.set() not r.set
@Harish-ou4dy
4 жыл бұрын
@@Codemycom BullsEye!! thanks ..
@Codemycom
4 жыл бұрын
@@Harish-ou4dy good :-)
My radio buttons get selected even without clicking on it. Just move near to radio button and it gets selected moreover both get selected at a time i.e. one doesn't get unselected when other one is selecetd. Help me. Below is my code: from tkinter import * def script(): return r = IntVar root = Tk() root.title('Renaming Parts') textbox = Entry(root, width=50) textbox.grid(row=0, column=0, columnspan=4) Radiobutton(root, text='WIRE', variable=r, value=1).grid(row=2, column=0, columnspan=2) Radiobutton(root, text='COMPO', variable=r, value=2).grid(row=2, column=3, columnspan=2) Enter = Button(root, text='Enter', width=25, command=script).grid(row=3, column=0, columnspan=2) Exit = Button(root, text='Exit', width=25, command=root.destroy).grid(row=3, column=3, columnspan=2) root.mainloop()
@Codemycom
4 жыл бұрын
Two things...your r=IntVar needs to be r = IntVar() you fogot the parenthesis. Next, make the radiobutton variable global. So above the RadioButton code, go: global r r = IntVar()
I got an error it says list object doesn't have get attribute .. how to fix this
@Codemycom
4 жыл бұрын
check your code for typos
"which is insanely cheap " tone makes me laugh🤣🤣🤣🤣🤣🤣🤣🤣
@Codemycom
4 жыл бұрын
Ah, but did it make you buy? LMAO
gender=StringVar() Radiobutton(root, text='Male', variable=gender, value='Male').place(x=150, y=220) Radiobutton(root, text='Female', variable=gender, value='Female').place(x=220, y=220) Both the radio buttons selected by default for me. How to resolve that ?
@Codemycom
2 жыл бұрын
You didn't set them...rewatch the video.
Sir , I'm getting AttributeError: 'NoneType' object has no attribute 'get'
@Codemycom
4 жыл бұрын
you need to do .get() not .get and make sure not to .pack() on the same line
@subhasiz
4 жыл бұрын
Thank you sir, it worked
@Codemycom
4 жыл бұрын
@@subhasiz Great!
I tried doing the StrVar() but it says it is an undefined variable
@Codemycom
4 жыл бұрын
Check your code for typos
@yukidango6730
4 жыл бұрын
@@Codemycom I just corrected it, it isn't StrVar() it is StringVar() xD
@Codemycom
4 жыл бұрын
@@yukidango6730 ;-)
sir I am facing a problem for me when I open my window all the the radio buttons are already pre selected plss help
@Codemycom
3 жыл бұрын
did you not change something in each of the radio button code?
@sudeepsanthosh8819
3 жыл бұрын
@@Codemycom no
I have written the following code. I haven't set the value of r but still, in the beginning all the options are shown as selected. r = StringVar() Radiobutton(frame,text="option 1",variable=r,value="1").pack() Radiobutton(frame,text="option 2",variable=r,value="2").pack() Radiobutton(frame,text="option 3",variable=r,value="3").pack() I followed your code. In your output, it doesn't show selected if its not set. But I don't understand why in mine all the options are pre selected. Is there a way to remove those?
@Codemycom
4 жыл бұрын
You need to set one.
@PraptyChanda
4 жыл бұрын
@@Codemycom okay will try that. Thank you!
my code doesn't like the "command =Lambda:" even though I have imported from _ast Lambda from _ast import Lambda from tkinter import * from PIL import ImageTk,Image root = Tk() r = IntVar() #r.set("2") def clicked(value): myLabel = Label(root, text=value) myLabel.pack() Radiobutton(root, text="Option 1", variable=r, value=1, command=Lambda: clicked(r.get())).pack() Radiobutton(root, text="Option 2", variable=r, value=2).pack() mainloop() It claims that Lambda is an invalid syntax.
@Codemycom
3 жыл бұрын
Don't import Lambda. Your code needs lowercase l in lambda, not uppercase
@sushmachandran2179
3 жыл бұрын
@@Codemycom That fixed it. Thanks a ton!
@Codemycom
3 жыл бұрын
@@sushmachandran2179 Sure thing
Why using mainloop() Instead of root.mainloop() In las line
@Codemycom
Жыл бұрын
should probably be root.mainloop()
Why we can't .pack() at the same line?
@Codemycom
4 жыл бұрын
Because it confuses tkinter if there's a function to call.
@alitalhaoylek2413
4 жыл бұрын
@@Codemycom thanks
@Codemycom
4 жыл бұрын
@@alitalhaoylek2413 sure thing
my button does not deselect when I click the other
@Codemycom
3 жыл бұрын
Check your code for typos
@just_all_in
3 жыл бұрын
@@Codemycom my code is from tkinter import * root = Tk() root.title('EX001') r = IntVar Radiobutton(root, text='op01',variable=r,value=1).pack() Radiobutton(root, text='op02',variable=r,value=2).pack() mainloop()
I like you, can you reply me!!!
Second
@Codemycom
5 жыл бұрын
ha!
lol the name of your PC is flatplanet? Didn't know that you were a flat earther 😁
@Codemycom
4 жыл бұрын
I'm not, I just find it hilarious that some people do so I named my computer that lol
@Skyvastern
4 жыл бұрын
@@Codemycom yeah I got that feeling that it was named as a joke xD
Some error about command=Lambda:
@Codemycom
4 жыл бұрын
lambda should be lowercase
Seems like you have crammed your advertisement. '''Which is insanely cheap xD'''
@Codemycom
3 жыл бұрын
huh?
Can you please review my code.?
@Codemycom
3 жыл бұрын
Sorry I don't do that
First lol
@Codemycom
5 жыл бұрын
Ha!
how to destroy radio buttons?
@Codemycom
3 жыл бұрын
.destroy()
@shubhankarpanda9687
3 жыл бұрын
@@Codemycom it is not working in for loops as well as in single creation
@shubhankarpanda9687
3 жыл бұрын
@@Codemycom can you please refer some documentation on this
@Codemycom
3 жыл бұрын
@@shubhankarpanda9687 Check my playlist...I have videos on destroy.
@shubhankarpanda9687
3 жыл бұрын
@@Codemycom ok sir thank you
Can I Copy this code?? Please send the Code
@Codemycom
4 жыл бұрын
Don't be lazy.
@bxstiii
4 жыл бұрын
@@Codemycom have now written to me the code and it has helped me can you be reached on discord ??
@Codemycom
4 жыл бұрын
@@bxstiii No, sorry
to long
@Codemycom
4 жыл бұрын
incorrect