create account

Python Private Lessons / Series 4 by kingmaggot

View this thread on: hive.blogpeakd.comecency.com
· @kingmaggot · (edited)
$4.23
Python Private Lessons / Series 4
**What will we learn?**

- Python Private Lessons / Series 4 ( Window construction for the operating system )

<br>

https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Python_logo_and_wordmark.svg/2000px-Python_logo_and_wordmark.svg.png

<br>

**Requirements:**

- Notepad+
- Operating System 
- Python
- PyCharm Edu [Download](http://www.jetbrains.com/pycharm-edu/download/download-thanks.html?platform=mac)

<br>

**Difficulty Level:**

- Normal Level

<br>

**Let's Start the Course :**


**Button**

All the features we see in the label are also available in the button. That's why I'm not going to repeat what we said about "label." Here we will talk more about the different aspects of the button.

Unlike labels on buttons, there is a Click event. We can run a function every time you click the button. We will use the "command" parameter to assign functions to the button.

``` python
from Tkinter import *

window  =  TK()
window.title("Window Title")
window.geometry("200x200+300+100")

def write_message () :
     print("Button Pressed.")

button = Button (

    text   =   "Messange",
    command  = write_message

)

button.pack()

mainloop()
```

Message from console when clicking the button :

``` language
Button Pressed
```

![3.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092923/rgzigyakqd5yhcvsb2qo.png)


The more we squeeze the button, the more consonants will be written. So every time you click the button, This function starts working from the beginning.

**Change Any Element By Pressing The Button**

When the button is pressed, we can rearrange any of the elements in the window. There is no need to press this button to make this change, but we will be in this direction for example because we are about buttons.

For example, let's get a label in the window and when we click on the button, change the text in this label.


``` python
from Tkinter import *

window  =  Tk()
window.title("Window Title")
window.geometry("200x200+300+100")

def change_post () :
     label ["text"] = " Hello Utopian"

label   =  Label (text = " Hello Utopian / Kingmaggot ! ")
label.pack ()

button = Button (

    text   =   "Messange",
    command  = change_post

)

button.pack()

mainloop()
```
![Screenshot at Feb 20 03-10-25.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092658/xcvxc0te3xyl9niydiqq.png)

 will be written on the "Hello Utopian" program before the button is pressed.

![1.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092729/vnkdljogtk5l5aanjn1t.png)


When we click the button, "Hello Utopian / Kingmaggot!" Will be written.

In this way, we can easily change the other features of the elements.

**Button Status**

Buttons have their states. The same goes for label. However, the change can be seen more comfortable with the button.

The event that we call the situation refers to the active, passive or normal shape of the button. For example, if a button is passive, we can't click it. When creating the button, we use the "state"parameter to set the button in the situation. As with other features , we can do this when creating the button, or we can change it after creating the button.


``` Python
from Tkinter import *

window  =  Tk()
window.title("Window Title")
window.geometry("200x200+300+100")

def change_post () :
     label ["text"] = " Hello Utopian"

label   =  Label (text = " Hello Utopian / Kingmaggot ! ")
label.pack ()

button = Button (

    text   =   "Change",
    command  = change_post,
    state  = "disable"

)

button.pack()

mainloop()
```

The button should not be clicked because we set the status of the button to "disable". The program looks like this:

![4.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519093212/ezeykfjrndyxlpfirn8j.png)

We can also give "active" or "normal" values. In this case, we can give a different style. You can also see this with the comments in the following code.


``` Python
button = Button (

      text  = "Change",
      command = "change_post"
      state   =  "disabled",

      disabledforegground       =       "red",

      activeforegground             =       "black",

      activebackground       =       "yellow",
)
```

**Press the button with the Software**

normally, a button needs to have someone pushing it to do its job. However, we can operate the button without having to click it. After creating the button, let's run the function "invoke()" and look at the result.


``` python
from Tkinter import *

window  =  Tk ()
window.title ("Window Title")
window.geometry ("200x200+300+100")

def change_post ():
     label ["text"] = " Hello Utopian"

label = Label (text = " Hello Utopian / Kingmaggot ! ")
label.pack ()

button = Button (

    text   =   "Change",
    command  = change_post,

)

button.pack()

button2.invoke()

mainloop()

```

![5.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519094117/t44a0duwtco1tcxeak7p.png)


You know how we've written this program before. Normally, it was supposed to say "hello utopian." Now, as soon as he opened the program, he clicked the button.


**Turn off the program with the button**

We can use the "quit()" function to close the program with the button. You can see a sample code below.

``` Python
from Tkinter import *

window  =  Tk ()
window.title ("Window Title")
window.geometry ("200x200+300+100")

def close ():
    quit()

button = Button (

    text   =   "Close",
    command  = close,

)

button.pack()

mainloop()
```

![6.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519094300/ljke1apf41g1cgvvica4.png)


**For the rest of the article, follow our series.**

<br>
**Series :** <br>
1 - [Python Private Lessons / Series 1](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-1) #1 <br>
2 - [Python Private Lessons / Series 2](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-2) #2 <br>
3 - [Python Private Lessons / Series 3](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-3) #3 <br>
4 - [Python Private Lessons / Series 4](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-4) #4 <br>
5 - [Python Private Lessons / Series 5](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-5) #5 <br>


<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-4">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorkingmaggot
permlinkpython-private-lessons-series-4
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":81598961,"name":"cpython","full_name":"python/cpython","html_url":"https://github.com/python/cpython","fork":false,"owner":{"login":"python"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","button","python","article","kingmaggot"],"users":["kingmaggot"],"links":["http://www.jetbrains.com/pycharm-edu/download/download-thanks.html?platform=mac","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092923/rgzigyakqd5yhcvsb2qo.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092658/xcvxc0te3xyl9niydiqq.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092729/vnkdljogtk5l5aanjn1t.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519093212/ezeykfjrndyxlpfirn8j.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519094117/t44a0duwtco1tcxeak7p.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519094300/ljke1apf41g1cgvvica4.png","https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-1","https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-2","https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-3","https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-4","https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-5"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092923/rgzigyakqd5yhcvsb2qo.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092658/xcvxc0te3xyl9niydiqq.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519092729/vnkdljogtk5l5aanjn1t.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519093212/ezeykfjrndyxlpfirn8j.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519094117/t44a0duwtco1tcxeak7p.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519094300/ljke1apf41g1cgvvica4.png"],"moderator":{"account":"yokunjon","time":"2018-02-20T04:24:33.717Z","flagged":true,"reviewed":false,"pending":false},"questions":null,"score":null}
created2018-02-20 02:42:24
last_update2018-02-22 21:10:39
depth0
children4
last_payout2018-02-27 02:42:24
cashout_time1969-12-31 23:59:59
total_payout_value3.101 HBD
curator_payout_value1.127 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,448
author_reputation355,754,647,561
root_title"Python Private Lessons / Series 4"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,945,183
net_rshares943,089,509,034
author_curate_reward""
vote details (58)
@bidbot · (edited)
<b>This post was upvoted 25+ by @bidbot. Thank you for using me, see you.</b><br>
<a href="https://steemit.com/cointurk/@bidbot/about-bidbod-and-usage"><img src="https://steemitimages.com/100x50/https://i.hizliresim.com/2JEd0d.png"></a>
properties (22)
authorbidbot
permlinkre-kingmaggot-python-private-lessons-series-4-20180220t025158736z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["bidbot"],"image":["https://steemitimages.com/100x50/https://i.hizliresim.com/2JEd0d.png"],"links":["https://steemit.com/cointurk/@bidbot/about-bidbod-and-usage"],"app":"steemit/0.1"}
created2018-02-20 02:52:06
last_update2018-02-20 02:54:12
depth1
children0
last_payout2018-02-27 02:52:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length236
author_reputation1,794,803,451
root_title"Python Private Lessons / Series 4"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,946,916
net_rshares0
@luckypower ·
Thanks for the information. This is a very helpful post.
properties (22)
authorluckypower
permlinkre-kingmaggot-python-private-lessons-series-4-20180220t024306857z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-02-20 02:43:09
last_update2018-02-20 02:43:09
depth1
children0
last_payout2018-02-27 02:43:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length56
author_reputation-973,981,471,413
root_title"Python Private Lessons / Series 4"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,945,297
net_rshares0
@steemitstats ·
@kingmaggot, No matter approved or not, I upvote and support you.
properties (22)
authorsteemitstats
permlink20180220t024541822z-post
categoryutopian-io
json_metadata{"tags":["utopian-io"]}
created2018-02-20 02:45:48
last_update2018-02-20 02:45:48
depth1
children0
last_payout2018-02-27 02:45:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation351,882,871,185
root_title"Python Private Lessons / Series 4"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,945,749
net_rshares0
@yokunjon ·
Your contribution cannot be approved because it does not follow the [Utopian Rules](https://utopian.io/rules).

Violated Rule:

* Design or video editing related tutorials, gameplay, simple on-screen instructions, ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will not be accepted.

My Opinion:

* A tutorial must be informative and explanatory, but also "tutor". This tutorial lacks "tutor"ing, and it is nothing more than an explanation of a documentation.

* We prefer explanations of such concepts like Tkinter as a whole. Instead of making 20 parts out of Tkinter, it is preferable making a full tutorial with the explanation of Tkinter with useful (not "hello world") examples.

* Still same with **[part 3](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-3)** and **[part 2](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-2)**.

You can contact us on [Discord](https://discord.gg/uTyJkNm).

**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authoryokunjon
permlinkre-kingmaggot-python-private-lessons-series-4-20180220t042536431z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-20 04:27:06
last_update2018-02-20 04:27:06
depth1
children0
last_payout2018-02-27 04:27:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,070
author_reputation19,266,807,595,513
root_title"Python Private Lessons / Series 4"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id38,965,472
net_rshares0