Creating Buttons With TKinter - Python Tkinter GUI Tutorial #3
HTML-код
- Опубликовано: 20 авг 2024
- How to create buttons with TKinter and Python. In this video I'll show you how to create buttons with tKinter. Its pretty easy!
In this series I'll show you how to create graphical user interfaces for Python with Tkinter. TKinter comes with Python already, so there's nothing to install!
✅ 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 RUclips Channel:
bit.ly/2IGzvOR
▶️ Learn to Code at Codemy.com
Take $22 off with coupon code: youtube
▶️ Watch Entire Tkinter Playlist ✅ Subscribe To My RUclips 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
@poodles Game sure thing
what if the button's command is a function that requires me to send a value? I can't use the bracket cus the function will just start. How should I do it? Or if u made a video about it can u link it to me pls
@@iancheng6861 use lambdas...yeah I've done videos on it. command=lambda: function_name(pass stuff)
@@Codemycom thx thx
@@iancheng6861 sure thing
Such a friendly ,smooth and clear explanation , I'm really having fun with it. Thanks a lot.
Thank you! I appreciate that!
Thank you. I really like your teaching style. You come across as very friendly and patient. It makes me want to learn more from you.
Glad you're enjoying them!
You're kind of like the best thing that ever happened to youtube :)
Wow, thanks!
Unleash your Tech bruh, no need to use foul language
@@atakheli3608 its not me my bro he has no idea how to code in python then he learns from RUclips but makes slightest of mistakes and does this shit from my account
Unleash your Tech oh ok, hopefully he he gets better at code
@@atakheli3608 he just made tic tac toe its on the App Store I helped him a lot but yah
I just started with Tkinter today and i already know how to make a button. Good video and i think its the best explanation i've seen so far!
Good job, keep it up! :-)
I learned how to use Python GUI in just a few hours, just amazing.
Awesome!
I really really appreciate your content, BIG TIME❤
Glad you enjoy it!
really liked the way you teaching this, when i am not broke and have extra time i will surely check out your courses.
Thanks!
Awesome lectures, even a beautiful teacher taught amazing lessons.
Thanks!
Hi, thank you so much for your videos, they are really useful!
I love the way you explain everything.
I found it easier to write everything in Visual Studio Code and then just "run" it from there instead of using the Windows Terminal and always typing the command.
I like to use as many shortcuts as possible, I do like Sublime text too but since VS I find everything easier on it.
Glad you enjoy the videos :-)
Great job deserving of thanks
عمل رائع يستحق الشكر
انا اتفق معاك
thank you for this video! im doing A personal project and I rlly want to immerse myself with GUI specifically tkinter and i already have good feeling about it, thank you
Glad you're enjoying it!
Your videos are great. I love your way of teaching. Thank you so much John for providing this for free!
Glad you enjoy them!
thank you so much for replying to every comment it is so cool!
My pleasure!
I am exactly the person who you made this video for and who would change the color of buttons and make them bigger lol I am legally blind and starting to work on creating a customTkinter GUI for the visually impaired and a large orange send button would be very helpful :-) thanks for the info I'm going to watch all 200 and whatever videos lol I probably won't come up with a working app but you'll get watch time haha
Nice!
Great lessons my friend, you're a good person!!
Thanks!
It very difficult in my country to get teachers like you....... I'm from India Love you sir
thanks for this
You're very welcome
Really useful for my next seminar in class. Thank you
Happy to hear it!
Now I am going to beat my friend's challenge! All thanks to god, then to you!
ha fun
What else can i say... thank you soooooo much. you the man.
Thanks!
Button padx, pady are not to change size of button. They are to add extra padding around button text
which changes the size of the button. why argue semantics that are meaningless?
Cheers! For my 1st Button with you!
Awesome
You actually blew my mind 😂
glad to hear it! lol
Very interesting. I am running this on an Orange Pi-5 Debian-Bullseye 64-bit OS. Now I need to figure out how to make the OPi5 OPi.GPIO Python3 PWM pins run the servo Pan/Tilt motors? They are a little different than the Raspberry Pi4 setup. It would be cool to control the servos with some TKinter buttons. 😎 Thank you.
Sir I'm Your New Student Love From Pakistan
Welcome!
thank you so much
!!!!!
Welcome!
thanks alot this was easy and fast to learn and i feel like i have acheved somethign, thanks!
You're welcome!
I want to add some kind of light besides a button(s) and each time the button(s) was(were) clicked the light jumps from red to green and in case an error occurs or the function couldnt be executed the light stays red. Any idea how to tackle that? Thanks a lot for the help in advance!
I understood very clearly
Nice
would love to see a video on Websockets in Python via flask/dejango
I'm planning on doing a lot more flask videos soon
Thanks to this I have a wall of buttons
awesome
Your videso are great!! Continiues with thes god work!
Thank you so much!
This guy is seriously awesome haha
hey thanks!
Lesson check. Thanks.
welcome
Great tutorial, the only thing is that the fg="blue" works fine, but bg="red" does not change the button colour. Both parameters are coma separated. I'm on Mac.
You must have a typo...did you forget to wrap red in quotation marks or something? You can also use hex color codes.
You're really a magician.
Thanks!
@@Codemycom 😊You deserve it
@@abhijitjagdishprakash4104 I appreciate it!
loving this!
I appreciate your teaching
Glad you're enjoying it!
thank you for your help.❤❤😊
welcome
how to deal with the problem of button resizing with window resizing using grid?
Are you asking how to make the window bigger while the button maintains its current size?
Hi, the videos are all good, thank you very much.
Best way to teach. Go on.
glad you enjoyed it, there's almost 200 more videos on this playlist
I imagine the function call parentheses are left off because the statement command already denotes a function. Weird little quirk.
Why does 'my_click' work differently than 'my_click()'?
thank you for these videos. very informative
Glad you like them!
Is there a video in your playlist, that goes more in depth so that i can place things in my program more accurately, maybe even click and drag things into a place... maybe using coordinates or something, then you type them in? thx
sure
this is a simple way to learn python .
thanks
When using bg to change the color it isn't working for me at all; fg works completely fine but bg doesn't change anything. Do you know why that may be happening? Appreciate the videos :)
had the same problem:-(
this guy is rock !
Thanks!
very clear explanation awesome channel, I'm gonna SUBSCRIBE! and like
Thanks!
sir thanku that ( ) guide help me alooooot thankuuu
Welcome!
Thank you🙏
Welcome!
Is there a way to increase the feedback when you click a button? When I click a button you can barely tell that you clicked it. Thank you for all your videos :-)
Can we change the shape of the button too?
Plz help....
Not really...you can use an image and then bind that to a click, but it's a hacky sort of thing...
Thank you
welcome
Hey i want to pass an argument while we click a button, how to deal with that case, please help anyone
Use a lambda, I have many videos on that in the playlist
Thanks for the vid
You bet
شكرا كثيرا perfect
Is the a way to put a Checkmark (ALT 0252) on the button?
Thank you!
Even when I put in the root.mainloop(), there's no feedback when I press the button. Why? (I'm on Mac btw)
even? You always have to put the mainloop no matter what...that has nothing to do with the button. Did you give your button a command? There's likely a typo in your code
@@Codemycom Here's my code:
from tkinter import *
root = Tk()
def click():
label = Label(text = "You pressed me!")
label.pack()
button = Button(root, text = "Press me!", command = click)
button.pack()
root.mainloop()
@@_fluffyy1028 that code works fine. Are you using other tools than I use?
@@_fluffyy1028write root in Label
Cảm ơn bạn!
Do we have a way of aligning a text inside a button?
Great..Improve👍
:-)
how to pass parameters to the function which will execute after button click?
use a lambda
whats up guys. its john elder... we all come for this intro lol
haha
thank you
Welcome
Hello sir, I am facing a small issue while implementing the code. I have created the click button, but when I click, the command message is printing in my terminal and not inside Tkinker window. Can you please help me which this issue.
rewatch the video and follow the directions exactly. There's no way what you're saying could possibly happen unless you radically changed the code from the video.
How can we put picture in our button?
Is that possible? 😅
Yes. I have videos on that in the playlist
in dutch we say y as the same as we say an egg, ei. So the joke is, if you smash an egg to a wall, the egg white and yolk will flow down. So thats the trick how i always get it xd. if you dont get it, search for the translation how we say y and egg.
ha!
My button doesn't show any up or down state when I click on it. Im using the exact code in your buttons video, The command function works though. I'm using PyCharm on a Mac. Any ideas?
After more testing, I needed to add "from tkinter.ttk import *" below the original "from tkinter import *". Maybe I'm running a later version?
@@scot1711 no, version shouldn't have anything to do with it.
Thanks. Спасибо.
Welcome
Howdy partner!
Hello!
@@Codemycom I was wondering if you could please make another tkinter tutorial with the new python update
@@Pablo-fo4em what new python update?
Hi Mr. Elder, thank you for these great videos. I have a problem with my code in that I can't get the background color to take effect in the button. And the button doesn't depress like it should. Otherwise, all functionality is in place.
Also I'm running on Mac OS. Thanks for any help.
What does your button code look like?
@@Codemycom have the same problem. My code is:from tkinter import *
root = Tk()
myButton = Button(root, text="Press me", fg="blue", bg="green")
myButton.pack()
root.mainloop()
i had the same issue where it doesn’t press, all you have to do is click out of the tkinter window and click back in, as for the other one i have the same issue and dont know how to fix it
Hello Mr. John and everybody.
First, thank u for this tutorials. And my question : "How can i add two mission to one button. I mean, How can i add two command ? This is my question. Thank from now.
Have the button command call a function, inside that function call another function
@@Codemycom Hi sir, your lessons are extremely helpful. I watched many videos on youtube for Tkinter however, I was not able to understand them. Your lessons are very clear and concise. Just a query, can we bind multiple functions to one command?Thank youu:)
@@prithikannan8568 you can add functions inside of functions in the normal python way you call any function
hey, almost nothing is working when I try to change the bg color of the button-neither the hex color codes or the color names. Is there anything I might've done wrong?
Yeah, probably everything. The code must be exactly like mine...
Thank
Welcome
Your video is very well explained.
If I add a fews entries, then a value to each of them. How can I get each of the values and add them up?
Here are the codes. Thanks
from tkinter import *
root=Tk()
somme=0
def add_button():
global somme
somme +=1
for i in range(somme):
lab1=Label(root, text='lab1')
lab1.grid(row=i, column=1)
entry1=Entry(root)
entry1.grid(row=i, column=2)
bouton_add=Button(root, text='Create widget', command=add_button )
bouton_add.grid(row=5, column=0)
root.mainloop()
Keep watching the videos in this series, I go over all of that.
Hello, the button works nicely but i'm facing a problem : I made it so when you click the button it makes you select a file and then it opens it. What i want is also the filePath to be stocked inside a global variable, but whenever I create a variable in the myClick function, I'm unable to reuse it outside of that function, any solution to that problem?
make it global inside that function, define it globally there
Thank you sir
Welcome
My button works , even though I forgot to add the root to it , can you explain why? I really don't understand what's happening.
No clue
share your code with us.
I am create a flutter app using my python code, so a simple app with a button that will open the python code. can you help?
No, I don't have any videos like that
at 6:18 when you click the button it adds to the previous line, how can I make it replace it instead?
and great teaching, thanks ^^
You can update anything with .config()
Can someone help? Why does GitBash stop processing after one run of Python command?
It doesn't stop...it is running your tkinter program. While it is running that program, it can't do anything else. Close the tkinter app and your gitbash will continue working.
here is the thing.
I have a label on the top and two buttons below that label. I also aligned them in the middle, like in the illustration below.
MAIN MENU
button1
button2
Now, I want to give space between that label and the buttons. And this is how I want it to look.
MAIN MENU
button1
button2
But each time I add "pady=50" in button 1, the output becomes like this.
MAIN MENU
button1
button2
Anyone, is there a way to modify the distance between button1 and button2? I want to keep the distance of my button1 now, what I want now is to move button 2 closer to button1. Please help me how to do it through a python tkinter code.
You can pass a tuple to your pady that only gives padding on one side. so pady=(0,50) or pady=(50,0) that should do the trick.
How can I create multiple buttons & assign them different commands??
What basically is my problem that -
I have a list wherein I have the text to be displayed on the buttons, & I want them to be displayed... And on clicking a particular button that button gets removed from the display...
Please provide me a solution to this 🙏
keep watching the videos in the playlist.
yes, at 8 minutes, you are not calling the function (command=myClick) you are 'passing a reference of the myClick function to command, like assigning a value to a variable...'
not really...you're calling the function...you're causing the function to be executed.
@@Codemycom by the way, I like your videos and plan on working through the whole set, then I see what you have on codemy ...I'm an 'old school' c/c++ (college days...) coder trying to 'knock some rust off my skill set' thanks
cool!
thanks
thanks
Welcome
@@Codemycom Can you halp me in next video I write my problem into comment in next video
@@skfungaming1308 only if it directly relates to the next video
do you konw why I cannot click my button?
Done
i get this error help me :
ERROR:
File "C:/Users/Fahima/Desktop/MakingButton.py", line 1
from tkinter import
^
SyntaxError: invalid syntax
MY CODE:
from tkinter import
root = Tk()
mybutton = Button(root, Text="Click me ")
mybutton.pack()
root.mainloop()
I use pycharm and i am not that good at codding i am just a beginner and i use python 3.8.5
can someone help?
line one is wrong. It should be
from tkinter import *
what if the button's command is a function that requires me to send a value? I can't use the bracket cus the function will just start. How should I do it? Or if u made a video about it can u link it to me pls
use lambdas...yeah I've done videos on it. command=lambda: function_name(pass stuff)
are Button, Label and others widgets classes or functions as you are saying in this video?
they are classes
John, do you have any guide for how to make buttons the same size/center the text? I'm having a hell of a time getting my buttons to be the same size (irrespective of the text inside), even with padx...
No sorry...I mean, they wouldnt be the same size because it depends on the text and always will
@@Codemycom Okay, thanks for answering.
i have a question, i am planning to learn to program with a desktop application as my end goal, now heres the thing, for what i need this program to do it will use SVG extensively for the main interactive aspects, its going to be based on the LCARS interface which are basically single colours and very simple shapes, my question is can it be done, if you google LCARS you can see exactley what im looking at doing, i just dont want to invest a ton of time in a pointless excersise, why have i chosen to go down the SVG route, performance for one, efficiency for another and aesthetics, i hope you can answert this question.
Sorry, I don't know what any of those things are...
I was wondering why when I clicked the button on a Mac, the window flickers black for a second then turns to normal. I assume that this is just the window being updated, but I was wondering if there is anyway to get it to update smoothly like on windows.
Also I can't seem to change the background color of the button and I was also wondering if that is just a system difference.
No clue what's going on there...it should be smooth
Codemy.com It seems that as I continued through the playlist, the flickering just stopped. Maybe it’s because the page was updating from the button being clicked and due to a system difference
Can we display an already stored variable using label. For ex:
var = 5
label1 = label(root, text = var)
Will it work
Or what if i want to display the variable and some text?
Yes it will work. But more importantly...you could have simply tried it yourself faster than it took to write this comment and wait for an answer. You can concatenate text = var + " some text" as long as they are the same data type (for instance, both strings).
@@Codemycom thank you sir, actually I am quite new to GUI , and just watched the first three videos, so I wanted to clarify my doubts before I started to actually code, which I shall be doing shortly
@@a_22_romitbhaumik89 cool
I tried bg, but it didn't work for me. Any idea why?
Here's my code: myButton = Button(root, text="Click Me!", command=myClick, fg="blue", bg="red")
It should work. try activebackground= or background= or Background=
Try using highlightbackground=“red” instead
hey John, i just joined codemy and used the code here to get all the courses, however, i cant seem to find the tkinter in the list of courses? i'll just watch here while i wait for your response. Great content by the way! thanks!
Hey Vince...the tKinter videos are exclusively on youtube.
@@Codemycom Hey John! thanks for the response! i'll follow the training here then.. keep it up!
@@vinceantonio777 sure thing!