Python Private Lessons / Series 3

in utopian-io •  7 years ago  (edited)

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.

CodeExplanation
nUp
sDown
eRight
wLeft
neUp - Right
nwUp - Left
seDown - Right
swDown - Left
centerMiddle

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_Cursorlr_angle
arrowman
based_arrow_downmiddlebutton
based_arrow_upmouse
boatpencil
bogasitypirate
buttom_left_cornerplus
buttom_right_cornerquestion_arrow
buttom_sideright_ptr
buttom_teeright_side
box_spiralright_tee
center_ptrrightbutton
circlertl_logo
clocksailboat
coffe_mugsb_down_arrow
crosssb_h_double_arrow
cross_reversesb_left_arrow
crosshairsb_right_arrow
diamond_crosssb_up_arrow
dotsb_v__double_arrow
dotboxshuttle
double_arrowsizing
draft_largespider
draft_smalspraycan
draped_boxstar
exchangetarget
fleurtop_left_arrow
gumbytop_left_arrow
hand1top_left_corner
hand2top_right_corner
hearttop_tee
icontrek
iron_crossul_angle
left_ptrumbrella
left_sideur_angle
left_teewatch
leftbuttonxterm
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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

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

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

Your contribution cannot be approved because it does not follow the Utopian 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.

You can contact us on Discord.

[utopian-moderator]