Skip to main content

Gooey GUIs ,tkinter ,Label Tricks, Let's Talk About Text Baby,It All Adds Up,Packing

 Gooey GUIs

DISCLAIMER: As the title states, this can get gooey. TKinter totally sucks - if you'd rather move on to something more fun, click "mark lesson as complete" and move on to Day 70 to get back to the good stuff.
It's time to bring our programs into the early 90s as we learn how to create a Graphical User Interface (GUI) with a Python library called tkinter.

 
tkinter


tkinter is one of the more popular Python GUI libraries.
👉 When you start a tkinter project, you get some boilerplate, or starter code. 

import tkinter as tk
window = tk.Tk()
window.title("Hello World") # Sets the name of the window in the border
window.geometry("300x300") # Sets the size of the window in pixels
hello = tk.Label(text = "Hello World") # Creates a text box
hello.pack() # 'pack' places the element on the screen
button = tk.Button(text = "Click me!") # Creates a button
button.pack()
tk.mainloop() 
This code will produce a window that looks like this:


Play around with the size of the window to see the effect of changing the dimensions.

Label Tricks

👉 We can also use variables to pass strings into labels like this:

label = "Hey there world"
hello = tk.Label(text = label) 

👉 Now I'm going to use a subroutine that changes the text in the label when I click the button.

window = tk.Tk()
window.title("Hello World") 
window.geometry("300x300") 
label = "Hey there world"
def updateLabel():
  label = "Bye world!"
  hello["text"] = label 
  # Subroutine that updates the text in the label.
hello = tk.Label(text = label) 
hello.pack() 
button = tk.Button(text = "Click me!", command = updateLabel) # Calls the updateLabel subroutine when the button is clicked
button.pack()
tk.mainloop() 

👉 Let's try the same trick, only this time the label contains a number which increments with every button click. For this I need to use a global label variable
.
window = tk.Tk()
window.title("Hello World") 
window.geometry("300x300") 
label = 0 # Sets the starting label value to 0
def updateLabel():
  global label # Uses the values in the label variable
  label += 1 # Adds one to the value in the label
  hello["text"] = label 
  
hello = tk.Label(text = label) 
hello.pack() 
button = tk.Button(text = "Click me!", command = updateLabel) # Calls the updateLabel subroutine when the button is clicked
button.pack()
tk.mainloop() 
Adding Text
Let's Talk About Text Baby

We can add text boxes to our windows using the entirely obvious text command.
👉 Here's the code you need in isolation: 
text = tk.Text(window ,height=1, width = 50)
# Three arguments, name of the window to place the text box in, height & width.
text.pack 
👉 And here it is in context. 
window = tk.Tk()
window.title("Hello World") 
window.geometry("300x300") 
label = 0
def updateLabel():
  global label
  label += 1 
  hello["text"] = label 
  
hello = tk.Label(text = label) 
hello.pack() 
button = tk.Button(text = "Click me!", command = updateLabel) 
button.pack()
text = tk.Text(window ,height=1, width = 50)
text.pack
tk.mainloop()

This gives us a window with a text box like this:


It All Adds Up


Let's make our program add the number in the text box to the number in the label when the button is pressed.


👉 To do this, we need to change the updateLabel subroutine. Here's the code in isolation:


def updateLabel():
  global label
  number = text.get("1.0","end") # Gets the number from the text box (starting at the first position and going to the end.) and stores in the number variable
  number = int(number) #Casts to an integer. I've done this on a separate line to prevent the line above getting too complex, but you can combine the two.
  label += number # Adds the number from the text box to the one in the label.
  hello["text"] = label 
Placing Items
Our window does look a bit odd, doesn't it? Why have the button above the text box?


👉 We can simply change the order by defining the text box before the button in

 the code: window = tk.Tk()
window.title("Hello World") 
window.geometry("300x300") 
label = 0
 def updateLabel(): 
  global label
  number = text.get("1.0","end") 
  number = int(number) 
  label += number
  hello["text"] = label 
  
hello = tk.Label(text = label) 
hello.pack() 
text = tk.Text(window ,height=1, width = 50)
text.pack
button = tk.Button(text = "Click me!", command = updateLabel) 
button.pack()
tk.mainloop() 



Packing

We can add arguments to pack() to control the position of items in the window. Again, I'm just showing the relevant lines of code in these examples.


👉 Let's move the button to the bottom of the window.
button.pack(side=tk.BOTTOM)

👉 And the text box to the top to the left of the window.


text.pack(side=tk.LEFT)
You can also use TOP, RIGHT, CENTER to control location.
Unpacking
If we had several buttons, the default would be to put them one on top of another.
button = tk.Button(text = "Click me!",
command = updateLabel) 
button.pack()
button = tk.Button(text = "Another Button", command = updateLabel) 
button.pack()
button = tk.Button(text = "Last one", command = updateLabel) 
button.pack()



We can arrange them into a nicer grid layout, but to do this we have to completely remove pack and break the entire window into a grid.


👉 We then use row and column numbers (zero indexed remember) to place our elements. I've put the label in row 0, text box in row 1 and buttons in row 2.


window = tk.Tk()
window.title("Hello World") 
window.geometry("300x300") 
label = 0
def updateLabel():
  global label
  number = text.get("1.0","end") 
  number = int(number) 
  label += number
  hello["text"] = label 
  
hello = tk.Label(text = label).grid(row=0, column=1)
text = tk.Text(window ,height=1, width = 50).grid(row=1, column=1)
button = tk.Button(text = "Click me!", command = updateLabel).grid(row=2, column=0)
button = tk.Button(text = "Another Button", command = updateLabel).grid(row=2, column=1)
button = tk.Button(text = "Last one", command = updateLabel).grid(row=2, column=2)
tk.mainloop()





Comments

Popular posts from this blog

FOR LOOP , RANGE

  FOR LOOP  A  while  loop is perfect to use when we  don't  know how many times we want the loop to repeat.  If we have an idea of how many times we want the loop to repeat, we can use a  for  loop to loop code in exactly the same way the  while  loop did.  EXAMPLE :  for counter in range(10):   print(counter) RANGE  The  range  function creates a list of numbers in the range you create. If you only give it one number, it will start at  0  and move to a state where the final number is  one less  than the number in the brackets. In this case, the final number would be  9 .  EXAMPLE :  total = 0 for number in range(100) :   total += number   print(total)

ALL ABOUT WHILE LOOP

 WHILE LOOP  A  while  loop allows your code to repeat itself based on a condition you set.   EXAMPLE : counter = 0 while counter < 10:   print(counter)   counter +=1 Infinite Loop  You have to be  really  careful that you don't accidentally invoke an infinite loop! This is where the computer will loop code until the end of time. Without a break. Forever.  This is just saying "count to 10 by 1 each time." to make the loop end. Don't forget, if your  condition  is a  >  then you might need to  -= . This will subtract from the variable instead of adding to it.   EXAMPLE : counter = 0 while counter < 10:   print(counter)    counter += 1

IF AND ELSE STATEMENT

  If Statements    These statements are a bit like asking a question. You are telling the computer:  if  something is true,  then  do this specific block of code. Double equals ( == ) is asking the computer to compare if these two things are  exactly  the same.  EXAMPLE :  myName = input("What's your name?: ") if myName == "David":  What is else? IF  the condition is  not  met with the  if  statement, then we want the computer to do the  else  part instead. Likewise, if the condition  is  met in the  if  statement, then the  else  bit is ignored by the computer. The  else  statement must be the first thing  unindented  after the  if  statement and in line with it    EXAMPLE :  myName = input("What's your name?: ") if myName == "David":  print("Welcome Dude!")  print("You're just the baldest dude I've ever seen...