What will we learn?
- Python Private Lessons / Series 3 ( Window construction for the operating system )
Requirements:
- Notepad+
- Operating System
- Python
- PyCharm Edu Download
Difficulty Level:
- Normal Level
Let's Start the Course :
Size of Text Area
We are not talking about the size of the font here, but the size of the area it covers. By giving the background color to the text you will see better the change of values here.
We have 2 parameters to set the size. "width" and "height"
article = label (
text = "Hello World",
fg = "#cccccc",
bg = "#ae0000",
width = 0,
height = 10
)
Inline Gap:
The inner space in this space is not the text as it is in the same text area, but the margins of the text area. These margins are given in the text field. So if we have the background color of the writing, we can see that it grows.
We will give these properties again as parameter "padx" and "pady".
article = label (
text = "Hello World",
fg = "#cccccc",
bg = "#ae0000",
padx = 0,
pady = 10
)
After we wrote the code, we left as many spaces as we specified in the borders of the text area.
Font Settings
The text, size, font and thickness we have written so far have been adjusted automatically. We can reset them with the "font" parameter of the "Label ()" property.
article = label (
text = "Hello World",
fg = "#cccccc",
bg = "#ae0000",
padx = 0,
pady = 10
font = ("Open Sans", "30" , "bold" )
)
In the Font parameter we added the font, the font size and the format of the font respectively.
We can use "italic", "normal", "underline" or "overstrike" fonts instead of "Bold".
Width of Text
Let's say we have a word that does not fit in the window. Suppose a piece of text would fit in the window, carry the screen. We need to limit this field and shift the next line. We will do this with the "wraplength" parameter.
article = label (
text = "Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian",
fg = "#cccccc",
bg = "#ae0000",
padx = 50,
pady = 30
wraplenght = 100
)
As you will see, the lines will be shifted down one line. We can change the width by playing with the value in this parameter.
Text Alignment
Text left Alignment
article = label (
text = "Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot,Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian,",
fg = "#cccccc",
bg = "#ae0000",
padx = 50,
pady = 30
wraplenght = 100
justify = "left"
)
Text right Alignment
article = label (
text = "Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot,Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian,",
fg = "#cccccc",
bg = "#ae0000",
padx = 50,
pady = 30
wraplenght = 180
justify = "right"
)
Text Average Alignment
article = label (
text = "Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot, Kingmaggot,Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian, Hello Utopian,",
fg = "#cccccc",
bg = "#ae0000",
padx = 50,
pady = 30
wraplenght = 180
justify = "center"
)
The Position Of The Article In Itself
When we enlarge the width and height of the writing, the writing is standardized. We use the "anchor" parameter to move this text right-left, up-down. You can find the values that this parameter will have in the table below.
Code | Explanation |
---|---|
n | Up |
s | Down |
e | Right |
w | Left |
ne | Up - Right |
nw | Up - Left |
se | Down - Right |
sw | Down - Left |
center | Middle |
Let's paste the word up-right for an example.
from tkinter import *
window = TK()
window.title("Windows Title")
window.geometry("200x200+300+100")
article = Label (
text = "python",
fg = "#cccccc",
bg = "#cccccc",
anchor "ne"
)
article.pack()
mainloop()
Change the mouse cursor when the label is over
When we come to the label we may want to change the mouse icon. For example, we may want to place a finger symbol instead of the standard arrow icon, if we link at one time. We will do this with the "cursor" parameter.
First I give a list of all the cursor names. Here you can choose what you want
X_Cursor | lr_angle |
---|---|
arrow | man |
based_arrow_down | middlebutton |
based_arrow_up | mouse |
boat | pencil |
bogasity | pirate |
buttom_left_corner | plus |
buttom_right_corner | question_arrow |
buttom_side | right_ptr |
buttom_tee | right_side |
box_spiral | right_tee |
center_ptr | rightbutton |
circle | rtl_logo |
clock | sailboat |
coffe_mug | sb_down_arrow |
cross | sb_h_double_arrow |
cross_reverse | sb_left_arrow |
crosshair | sb_right_arrow |
diamond_cross | sb_up_arrow |
dot | sb_v__double_arrow |
dotbox | shuttle |
double_arrow | sizing |
draft_large | spider |
draft_smal | spraycan |
draped_box | star |
exchange | target |
fleur | top_left_arrow |
gumby | top_left_arrow |
hand1 | top_left_corner |
hand2 | top_right_corner |
heart | top_tee |
icon | trek |
iron_cross | ul_angle |
left_ptr | umbrella |
left_side | ur_angle |
left_tee | watch |
leftbutton | xterm |
ll_angle | - |
article = label (
text = "Python",
fg = "#cccccc",
bg = "#ae0000",
width = 50,
height = 50,
cursor = "cross"
)
You can use it in this way. When we run these codes, our mouse is in the form of "x".
Insert Picture
With the "Label ()" feature, we can add images to my window. Let's add a "GIF" to the window.
from tkinter import *
window = TK()
window.title("Windows Title")
window.geometry("200x200+300+100")
picture = PhotoImage(file = "utupian.gif")
article = Label ("image = picture)
article.pack()
mainloop()
I had an image file called "utopian.gif". Since I do not write the full path to this file, it must be in the same folder as the official program. You can add the full path to the pictures in other places on the computer.
When these codes are run, the specified picture will start to appear in the background of my window.
If you want to add Bitmap file instead of "GIF" file, you can use "BitmapImage ()" function instead of "PhotoImage ()" function.
We can add text inside the same "Label ()" feature and align the picture and text easily. As you are writing, we will use the "text" parameter. For the alignment operation, we will use the "compound" parameter.
You can see the values that the "Compound" parameter can take on the table.
Kingmaggot / utopian.io |
---|
top |
buttom |
left |
right |
center |
As an example, let's try "left" value. Here you have to be on the left side of the picture.
from tkinter import *
window = TK()
window.title("Windows Title")
window.geometry("200x200+300+100")
picture = PhotoImage(file = "utupian.gif")
article = Label (
image = picture,
text = "Test Article",
compound = "left"
article.pack()
mainloop()
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
Congratulations @kingmaggot! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of upvotes
Award for the number of upvotes received
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Your contribution cannot be approved because it does not follow the Utopian Rules.
Violated Rule:
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.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit