Python Private Lessons / Series 2

What will we learn?

  • Python Private Lessons / Series 2 ( Window construction for the operating system )



Requirements:

  • Notepad+
  • Operating System
  • Python
  • PyCharm Edu Download


Difficulty Level:

  • Normal Level


Let's Start the Course :

Window Status

The windows are visible on the screen. If some programs are full screen, some of them should never appear on the screen, and others may want to start on the taskbar as an icon. we can simply call them window states. We will use the "state ()" function to set these properties of the window.

normal window: 
windows.state("normal")

full screen window: 
window.state("zoomed")

The window that appears in the taskbar:
window.state("iconic")

No visible window:
windows.state("withdrawn")

This way you can set the state of your windows according to the pro- cess.

Window Properties

We will use the "VM_Attributes ()" function to set some window properties. All of the features we'll see in these subtitles will be relevant to this function.

Transparent Window

To transparentize the window, we need to write "-alpha" to the first parameter of the function and write how we want it to appear in the second parameter. We have to write values ​​such as 0.5 for visible 50%, and 0.3 for visible 30%.

window.wm_attributes("-alpha", 0.5)

Window always on top

Most operating systems have windows. These windows can stand on top of each other. At that moment, if the window is active, usually that window is at the top. We can change this so that our own window always appears on top. By clicking on other windows our window will continue to appear on top.

We will do this with the "-topmost" property of the same function.

window.wm_attributes ("-topmost" , 1 )

Once we've added it between our codes, we can test that the window is always up.

Full Screen Window:

We can use the "fullscreen" feature of this function at times when we want the window to be full screen.

window.wm_attributes("-fullscreen" , 1 )

After adding this in our code and running it, you will see that the window is full screen.

Finding Screen Resolution:

We can easily find the resolution of the screen of our computer as it is in the code below.

from tkinter import *

#We have changed the Tkinter treasure.
window = TK()

print ("Width      :    " + str(window.winfo_screenwidth()))
print ("Height      :    " + str(window.winfo_screenheight()))

#We made the window stay open all the time.
mainloop()

Output :

Width: 2560
Height: 1440

Find the location of the mouse cursor :

You can see how we picked the mouse position and printed it on the screen using the following codes.

from tkinter import *

#We have changed the Tkinter treasure.
window = TK()

print ("Left      :    " + str(window.winfo_pointerx()))
print ("Right      :    " + str(window.winfo_pointery()))

#We made the window stay open all the time.
mainloop()

Output :

Left: 542
Right: 1256

Removing the Window Bar

We can remove the bar-shaped region of the window header at the top of the window with the "overrideredirect ()" function.

from tkinter import *

#Putting the variable in the tkinter's treasure.
window = TK ()

#We set the window title.
window.title("Utopian Title")

#window size and position
window.geometry("200x200+300+100")

#We raised the window bar
window.overrideredirect(1)

#we made sure the window stay open all the time.
mainloop()

Multiple Windows in the same Program:

We have used the "window" variable in the window operations we have done so far. This variable represented the currently visible window. We can open multiple windows that are bound to the same program by creating more of this variable. We can design each of these windows separately. Let's create five separate windows and look at the views on the screen. I changed the sizes and positions of these windows. At the same time I gave one window transparency feature. As such, we can apply all of the properties we have seen and seen so far for each window.

from tkinter import *

window = TK ()
window.title("Utopian Title 1")
window.geometry("200x200+0+0")

window2 = TK ()
window2.title("Utopian Title 2")
window2.geometry("100x300+400+400")

window3 = TK ()
window3.title("Utopian Title 3")
window3.geometry("300x100+1000+1000")

window4 = TK ()
window4.title("Utopian Title 4")
window4.geometry("500x100+200+800")

window5 = TK ()
window5.title("Utopian Title 4")
window5.geometry("500x100+200+800")
window5.vm_attributes("-alpha" , 0.5)

mainloop()

Window Elements

LABEL ( Adding Labels )

Let's create a blank window and test the addition of labels on it.

As in most programmer's first programs, we will write "Hello World" in the first article in order to break the tradition.

from tkinter import *

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

article = Label(text = " Hello World ")
article.pack()

mainloop()

As you can see in the code, I created an "article" variable that has a "label ()" content. We will create a new variable for each label we add. We will use this variable to specify properties, like we did in the same windows.

We can add different prameters to the "Label ()" property. The only thing we needed for now is the "text" feature. We need to write here. Once we have used the "Label ()" property, we need to package that variable, so we have to make it ready to be placed inside the window. We do this with the "pack ()" function.

As soon as we have done all these things, we will see our writing updated on the screen. The position, color, size, etc. of the article came with standard values ​​by "Tkinter" as it is in the windows. You will see how we will change these values ​​as you write our articles.

Text Color

We will use the "fg" or "foreground" parameters in the "Label ()" property to change the color of our writing. He will not notice which one we will use.

article = Labet (
      text    =  "Hello World",
      fg        =  "red"
)

Once we've updated our text variable like this, let's look at the window again

We wrote "red" here as a color. Here we can write other color names like blue, yellow. However, I generally prefer to use color codes instead of using these words. Because these words are not usually in the colors I want. You can also look at the code below together with the color code. We should also remember to type the "#" when writing the color code here.

article = Labet (
      text    =  "Hello World",
      fg        =  "#ae0000"
)

Text Background Color:

In the previous topic, we replaced the font with the "fg" parameter. We will do the same to change the background. This time the name of our parameters will be "bg" or "background". Here again we can use color code and color names together.

article = Labet (
      text    =  "Hello World",
      fg         = "#cccccc",
      bg        =  "#ae0000"
)

For the rest of the article, follow our series.



Series :

1 - Python Private Lessons / Series 1 #1

2 - Python Private Lessons / Series 2 #2

3 - Python Private Lessons / Series 3 #3

4 - Python Private Lessons / Series 4 #4

5 - Python Private Lessons / Series 5 #5



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center