Python Private Lessons / Series 1

in #utopian-io7 years ago (edited)

What will we learn?

  • Python Private Lessons / Series 1


Requirements:

  • Notepad+
  • Python
  • Operating System


Difficulty Level:

  • Normal Level


Let's Start the Course :

Creating Window

We need a window to design the visual interface with Tkinter. We will build this design in this window. We can create a standard window with no properties at all with the following codes.

from tkinter import *
window = TK ()

Here we assign the contents to the window variable in the form of TK (). We can actually think of this TK () as the treasure of the Tkinter library. We will always use this window variable because many of the elements we add into the window are bound here.

When we run these codes a small window will open and close immediately. You may not even have seen it opened because it was immediately closed. It does not mean that these codes work wrong. Just as we set it up, our windows were created for a short period of time and the program was shut down because it did the job that was told. We need to keep this program open all the time. We can do this with the function below. We can add this function to the bottom of our codes.

mainloop ()

After we have placed it at the end of our code, we can run it again.

As you can see, we created one of the windows we constantly encountered in the computer environment.

We will not have an empty window because we have not been exposed to any detriment. The title field is set to "tk" by default, and the dimensions of the window come in the dimensions defined by the Tkinter module itself. In fact, when we examine these details, we can understand that this window is not ours but the "TKinter" module. We can arrange these details so we can handle the control.

Window Title

We can start with our "window" title, which is one of the most basic features of our window. We will use the "title ()" method, which is bound to the window variable we created earlier.

from tkinter import *

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

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

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

If we run our codes again after setting the title, the window we created will be confused.

Window dimensions and position

Since we do not have any details about window sizes in our code, our window is a size we do not want. We may want to enlarge or shrink these dimensions. For this we will use the "geometry" method in the "Tkinter" module.

Let's think we want to set the window's width to "500px" and the height to "100px". In this case, the Tkinter module should have a spelling style of "500x100" as we would like it to be. We will put this in the same "geometry" function.

window.geometry("500x100")

When you run the code, you will see that the window sizes change. You can try different values ​​as follows.

window.geometry("200x300")

With these values, we can see that there is a different appearance.

If you try these codes on your computer, you can see that these windows are sticking to the left and top of the screen. This occurs because we have not entered any details about the window position. To adjust the window position we need to adjust how far we move from the left and above. The reason why our windows stick together to the left and up now is that these values ​​are "0" as a standard.

To better compare it, we will first create a window with the standard value and show how it sticks to the corner.

Now you can see our window again by removing "300px" from the left, "100px" from the top. To zoom out we wrote the window dimensions inside the "geometry" function. Without deleting this, we need to write window position values ​​next to these values. Let's say our window sizes are "200x200". When we add in the window position, our function will be like this.

window.geometry ("200x200+300+100")

Let's examine this place now. We combined our window sizes with the "x" sign in place of the first "+" sign. Here, the first written value is the width and the second value is the height. We then combined the values ​​with the "+" sign as we separated the window positions by placing a "+" sign. The first value is the distance to the left of the screen, and the second value is the distance to the top of the screen.

After you write the code, you can see that your window is now stopped somewhere else.

The painting we have done so far has worked properly. But now we have a problem like this. For example, we set our window size to "200x200". After the program works, the user can easily zoom in and out of this window. This is a nice feature, but in some programs we may want the dimensions to remain constant and not be changed by the user. We can easily avoid this with the "resizable" function.

This function takes two parameters. We can prevent the size of the window from the first parameter window and the window size from the second parameter.

windows.resizable(False, False)

In this way, two parameters in a usage are "False", so the user will not be able to size it anywhere.

windows.resizable(True, False)

This way, if only the first parameter is set to "True", the user will only be able to resize the window's window because the first parameter represents the window.

window.resizable(False, True)

If we do the exact same thing, the user can only change the size of the window.

This way, we can block or allow the user. However, in some cases we may want to allow the user for certain intervals. For example, we can think that the window can be reduced to the smallest 100x100 size or up to 800x800 size. It is very easy to do them like the others.

We use the "maxsize" function to specify the maximum values ​​because the window can grow to "minsize" window to specify the smallest value for the window size. These functions have 2 parameters. Their first parameters are the most, and the second parameters are the height values. If we give "True" value to these parameters, we will remove the limit on that value.

windows.minsize(100, 100)

With this code, we specified that the window can be enlarged up to 800x800.

I can see the codes so far in one go.

from tkinter import *

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

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

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

#We did not resize the window.
#First parameter: Most
#Second parameter : Size
window.resizable(False, False)

#The smallest value of the window will be 100x100.
windows.minsize(100, 100)

#The maximum value of the window will be 800x800.
windows.maxsize(800,800)

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

Learning Window Sizes and Location

We may feel the need to query the size and position of the window we have created. We can get this information very easily with the following codes.

from tkinter import *

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

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

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

print("Calculating window size ( Utopian.iO) ")

move = window.winfo_width()
size = window.winfo_height()
left = window.winfo_x()
right = window.winfo_y()

print    ( "  Move    :    " + str(move)      )
print    ( "  Size       :    " + str(size)      )
print    ( "  Left       :    " + str(left)      )
print    ( "  Right    :    " + str(right)      )

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

We will write the code in this way and print "()". These output will come to the console screen, not into the window. We will see how to write in the window next time.

When we run these codes, the result on the console screen looks like this.

Calculating window size ( Utopian.iO)
Move: 1
Size : 1
Left : 0 
Right : 0

The values ​​we see here have nothing to do with our windows. Because these values ​​are the values ​​the program received when it was first opened. We updated these values ​​after the program was opened, but "Tkinter" did not update these values ​​within itself. To get the correct values, we need to use the "window.update" function before we get the values.

from tkinter import *

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

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

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

print("Calculating window size ( Utopian.iO) ")
window.update()

move = window.winfo_width()
size = window.winfo_height()
left = window.winfo_x()
right = window.winfo_y()

print    ( "  Move    :    " + str(move)      )
print    ( "  Size       :    " + str(size)      )
print    ( "  Left       :    " + str(left)      )
print    ( "  Right    :    " + str(right)      )

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

Let's look at the result in the console after we've placed the "update ()" function.

Calculating window size ( Utopian.iO)
Move: 200
Size : 200
Left : 100
Right : 100

You can see that the correct values ​​are written.

If you notice here, we can get the position of the entire window. In other words, the gray area on the window is filled with account. We need to use the "rootx ()" and "rooty ()" functions if we want to skip the empty space in the window, ie the actual program view location.

Add these codes and look at the results again.

from tkinter import *

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

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

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

print("Calculating window size ( Utopian.iO) ")
window.update()

move = window.winfo_width()
size = window.winfo_height()
left = window.winfo_x()
right = window.winfo_y()

in-window-left = window_winfo_rootx()
out-windows-right = window_winfo_rooty()

print    ( "  Move    :    " + str(move)      )
print    ( "  Size       :    " + str(size)      )
print    ( "  Left       :    " + str(left)      )
print    ( "  Right    :    " + str(right)      )

print    ( "  in window left    :    " + str(in-window-left)      )
print    ( "  out window right    :    " + str(out-windows-right)      )

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

Output:

Calculating window size ( Utopian.iO)
Move: 200
Size : 200
Left : 100
Right : 100
İn Window Left : 100
Out Windows Right : 122

When we looked at the values, there was no change on the left side due to the operating system that I used. Because on my system I do not have a border on the left side. When we look at the top value, it's 100 to 122. So the gray area at the top of the window is 22 pixels in size.

As you can see in the window, while using the "rooty ()" function while calculating the upper part of the window on the right side, the gray area of ​​the window is getting values ​​without joining the account.

In this post we made so much comment for now. I will guarantee you will come to the best places in this program by telling you part of the part of our NodeJs narration :) I will see you in our next post.

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

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Congratulations @kingmaggot! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Hey @kingmaggot I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x