Python Private Lessons / Series 4

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@kingmaggot·
0.000 HBD
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/>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,