Tkinter is Python's most popular Graphical User Interface (GUI) library. This topic explains the proper use of this library and its resources.
Python Tkinter : How to create a radio button in tkinter
Support the Channel šš:
Buy me a coffee ā : https://buymeacoffee.com/usandopy_
ā”ļø Connect with me on:
Instagram: https://www.instagram.com/pybeginners/
Twitter: https://x.com/joaofuti_
LinkedIn: https://www.linkedin.com/in/joao-futi-muanda-16b980175/
Website: https://www.usandopy.com/en
Thread: https://www.threads.net/@pybeginners
Show More Show Less View Video Transcript
0:00
hello guys welcome back to the channel
0:01
my name is so today we are going to
0:03
learn how we can create a radio botom
0:05
inside python t Okay so R boot are those
0:09
buttons as we can see here okay those C
0:11
that we can select any option that we
0:14
wish to so we are going to learn how we
0:17
can create those botoms in Python okay
0:19
so in this we are going to perform
0:21
operation like how to create those
0:22
botoms okay how to show the radio botm
0:25
in the window and how to get the
0:27
selected value from those radio botm in
0:29
the window okay in the python so for
0:32
that let's go for our test editor okay
0:35
which is here so those are the things
0:37
from the previous um class so let's
0:40
delete this guy boom we don't need it
0:43
and if I save this so I have to remove
0:45
this guys those guys
0:47
Al um okay this info we might need so
0:50
for now let's let's say one save okay if
0:53
I run this guy boom we have only this
0:56
and this guy okay this label we might
0:58
need it we might need
1:00
so in order to create a rate bom we have
1:03
to do something like this okay so now
1:05
this label we we don't need it I can
1:08
delete it no
1:09
problem by Buton we need because we need
1:11
to execute these functions okay say if I
1:14
run boom we have only Buton here so in
1:18
order to create a r button let's say
1:20
something like that okay let's create
1:21
one variable called raid okay let's say
1:24
rate one will be equal then we call the
1:27
function called radio okay radio BM
1:32
radio BM this way botm radio botm BM
1:37
open and close save if I run this boom
1:41
nothing will happen I just run to check
1:43
if our things is working properly or not
1:45
so after that we have to place this
1:47
radio inside of window comma then here
1:50
we can p we can give the options okay
1:53
for them test will be equal let's say
1:56
first radio Buton okay first so like
2:00
that first
2:02
and after that okay on this radio button
2:05
when we create them we have to give them
2:07
value they must return some value so
2:10
let's give value equal okay for this
2:13
ready bottom is value will be uh will be
2:17
one okay will be one this way save and
2:21
if I run this nothing's going to happen
2:23
as always so now let's place our ready
2:25
Buton inside of our windows so let's say
2:27
red one okay let's use this guy
2:30
you come here put Doon stain grow zero
2:35
okay save and if I run this boom now we
2:38
can see we have here our first R botom
2:41
okay this our first let's let's give
2:43
some font on it you come
2:47
here give it little space and if I past
2:50
oh oh I put two
2:52
comma save and if I run boom now it's a
2:56
little big we can see our first radio
2:59
bottom okay
3:01
AA if I click is it's printing this one
3:05
okay perfect so this is how we can
3:07
create a radio bom in Python T okay now
3:09
let's create a three radio botoms Okay
3:11
so that we can see how they work
3:13
properly if I save this okay Radio One
3:16
radio two and radio three so those guys
3:19
will be
3:20
two and these two will
3:24
be three save okay so this will be in
3:28
zero uh you can come on okay stay in
3:31
zero so you stay in one and you stay on
3:34
two okay perfect if I save and if I run
3:36
they'll be like a line like uh first
3:39
second and third so uh the thing on the
3:43
radio botom is that okay they cannot
3:45
have the same name all the same value
3:47
okay so you must to change the names and
3:51
the value in the R button they cannot
3:53
have the same value and the same name it
3:56
must be different a compos for them this
3:59
guy will be handling the value two and
4:01
this guys will be handling the value
4:04
three okay save if I run this okay if I
4:07
click here for example okay now you can
4:09
see it's showing different okay but when
4:12
they had the same names so the same
4:14
value save if I run this as you can see
4:17
if I click on one it's like selecting
4:19
everything because they they have the
4:20
same values okay it's returning the same
4:23
value so that's why they must have the
4:25
different values okay let's say this guy
4:27
will be having one this going be having
4:29
two uh let's change the name okay
4:32
two and this guy will be having three
4:35
let's change the name to three save if I
4:38
run boom Okay click this guy click this
4:41
guy click now we can click any of them
4:43
and we can get the values from any radio
4:46
Buton okay perfect
4:49
so uh the next thing to do
4:52
okay H will be how to get okay how to do
4:57
the radio event radio button event okay
4:59
okay so the radio bom it has like three
5:05
values that it can handle okay it can
5:07
handle integer value it can handle a
5:10
string value and also it can have a
5:13
Boolean value okay so let's first do
5:16
with integer value okay so in order to
5:18
use integer value okay in radi Bon we
5:21
have to do something like that okay when
5:23
we want to select something in radi
5:25
Buton so for that we have to do
5:28
something like that see
5:30
here on the top okay on the top already
5:33
one I'm going to create one variable
5:35
called selected okay selected select
5:38
this selector will be equal of int value
5:41
okay in value okay in VAR okay this way
5:46
this way so that means it can handle on
5:50
the integer values okay so after I
5:52
create this okay I'm going to come here
5:57
okay I'm going to come here I'm going to
5:58
create one new something like called
6:00
variable
6:02
variable a new variable this variable
6:05
will be equal that selected uh variable
6:08
that we just create okay comma something
6:10
like that if I save and if I run this oh
6:13
oh I commit I commit here
6:15
okay I commit error I commit error
6:19
because this guys this in is supposed to
6:21
be Capital not uh small as we can see in
6:24
VAR okay save and if I run this okay uh
6:27
nothing is going to happen okay perfect
6:30
so the same thing uh we are going to
6:34
give to all of them okay let me give
6:36
variable selected uh no come here
6:40
variable selected you come okay you can
6:45
see here perfo
6:48
comma you can come here perfo
6:52
comma yeah only yeah save okay
6:56
so for us to get this value when we
7:00
click on that bottom in that function
7:02
okay so we have to give Al we have to
7:05
pass the command okay we have to connect
7:07
them so let's get that command hello
7:10
okay command hello let's come here and
7:12
paste command hello save you come here
7:16
so for now let's remove this button we
7:18
don't need it okay save okay and if I
7:21
run this okay it will be something like
7:24
this if I select this guy okay first
7:27
boom see it's going to print this this
7:29
one okay so that means every time when I
7:32
select any R botom it's going to perform
7:34
it's going to execute this function okay
7:36
but since this function we are just
7:38
imping we just printing this okay as we
7:41
said okay since it's just printing this
7:44
one okay we are not getting any
7:46
information from those guy so for that
7:48
let's make some change also okay so
7:51
instead of it so let's remove this guy
7:54
here where is info we can just give this
7:56
selected okay we can give we can give
7:59
this guy okay we can keep this selected
8:01
come here okay dot get okay so that this
8:06
way you can get any information that
8:08
will be selecting on those radio bottoms
8:11
okay so from here we can print just this
8:14
one okay if I save this save and if I
8:19
run okay if I click here for Bo it's
8:23
going to print one okay and if I run
8:27
this it's going to print one also one
8:29
one one uh why is printing one let me
8:32
check the
8:33
things uh as we can see it's not
8:35
printing two why let me see I think I
8:37
forget something oh now I remember okay
8:39
I forgot to give this function hello to
8:42
all of them okay you come here you stay
8:44
boom okay you come stay boom Perfecto so
8:48
now let's save I think I did a mistake
8:52
okay no yeah okay you going to say there
8:53
but here okay you stay here and safe and
8:56
if I run this okay let me give a little
8:59
come up you come here and boom see if I
9:02
click on first boom it's getting the
9:04
first value if I click on two it's
9:06
getting the second value and so on okay
9:08
so it can just keep like that whatever
9:10
value I select it will ging it getting
9:13
the value that we select okay so the
9:14
value are those ones that we P here okay
9:17
so this way we can get the values and
9:19
manipulate as we wish Perfecto okay so
9:23
the second one is using string okay as
9:27
we said okay instead of in okay we can
9:29
use it string VAR okay string VAR I can
9:33
just come you come out I can say string
9:36
this way okay string VAR so here in
9:38
string VAR okay we must give string here
9:41
okay so let's St let's for let's keep
9:43
like that okay if I save this and if I
9:45
run and as you can see everything is
9:48
marked like that okay but it's working
9:51
it's giving one two three 4 but if I
9:53
remove this and if I save and if I run
9:56
you can see everything is coming like
9:57
marked like this okay but now let's
9:59
let's change here value we can give uh
10:03
we can give string values okay for let's
10:05
say here for
10:07
Angola and here let's
10:11
say uh which one which count is coming
10:13
on my head okay let's say
10:16
Brazil and which country here let's say
10:19
India India okay safe and if I run this
10:23
guys Perfecto if I click in the first is
10:26
going is giving Angola if I click this
10:29
is Ging giving Brazil is I click this is
10:30
giving India okay and so on so apart of
10:33
that also it uh as we said okay it can
10:37
handle three values in val int values
10:40
string values and bullan values also
10:43
okay so for boan values it's very easy
10:46
easy easy okay the same thing here you
10:49
come
10:51
out you come out and let's write it boo
10:55
okay boo boam how to write boo I forget
10:58
my God okay Boolean this way Boolean VAR
11:01
okay save so for if I keep Boolean VAR
11:04
and I give this values let me see save
11:06
and if I run if I try to click here boom
11:09
I'm going to get here okay because it
11:11
can accept only bullan values okay so
11:13
you come out so bullan values it can
11:15
accept only two types of value either
11:17
zero or one or or true and false okay so
11:21
let's say true then if I save and if I
11:25
run so if I click on first it's going to
11:28
give me true but if I click on two it's
11:30
going to give me
11:31
error because here in the value okay in
11:34
the second I give Brazil Brazil is not a
11:37
is not the bullan value
11:39
okay let's delete so true here let's say
11:43
F
11:44
okay F so which means we don't need the
11:48
third one only one zero okay save if I
11:52
run this uh let's say first second first
11:57
second okay so even Al you can give like
12:00
one one or zero okay because zero first
12:05
and two one zero save and if I run let
12:08
me see if I give this true see false
12:11
true FSE so one is true and zero is
12:13
false that's why but if I give for
12:16
example two let's let's give two okay
12:18
comma save and run this one is going to
12:22
work and this one is going to work also
12:24
so just keep since is two consider it as
12:27
a two two two so whatever number that we
12:30
give here it considered as a true let's
12:32
say eight save in the Run boom just
12:35
giving like true true true true but
12:37
let's try to give a negative number
12:39
minus8 save and if I
12:42
run same thing okay whatever number we
12:45
give is going to give true so but we
12:47
have to specify like zero sa and if we
12:49
do this so guys this is how we can
12:52
create a radio botom in Python so if you
12:54
like the video give a like to the video
12:56
and subscribe to the channel okay my
12:58
name is and see you in the next video
13:00
thank you and bye-bye
#Programming
#Software
#Scripting Languages

