COMPUTER SCIENCE CAFÉ
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY

FUN WITH CODING
TKINTER - STYLING OBJECTS

WHAT IS IN THIS SECTION

Picture
This section cover some styling elements when using Tkinter such as text, colour, buttons, images, banners and the use of frames and canvas to allow slick change of screens.
CHANGE TEXT FONT AND SIZE

You can change the font and size of the text you use without any additional library imports by using the method in the example below:
myText.config(font=("Courier", 44))
See an example used in the code below and feel free to copy and try it out.
Picture
from tkinter import *

screen = Tk()
screen.minsize(400,400)# H / V
screen.title("Changing the Font")
screen.configure(background="#71d0c8")

pageTitle = Label(screen, text = "This is my font")
pageTitle.config(font=("Courier", 35))
pageTitle.place(x = 20, y = 20)

pageTitle = Label(screen, text = "This is my font")
pageTitle.config(font=("Georgia", 30))
pageTitle.place(x = 20, y = 80)

pageTitle = Label(screen, text = "This is my font", bg = "#71d0c8")
pageTitle.config(font=("Comic Sans MS", 25))
pageTitle.place(x = 20, y = 140)
BUTTON STYLES AND SIZE

Button text align: anchor = N (also use S,W,E)
...
TIME DELAY A POP-UP MESAGE

This shows how using the .after method you can set a delay on an action. In this example the 'Welcome' message shows for 2 seconds then disappears.
from tkinter import *

screen = Tk()
screen.minsize(305,475)# H / V
screen.title("Using time delay")
screen.configure(background="#71d0c8")

userName = ("Bob")

def homeScreen(userName):
   welcome = Label(screen, text = "Welcome " + userName)
   welcome.place(x = 100, y = 40)
   welcome.after(2500, removeEntry)#Delays for 2500 the runs the function removeEntry

def removeEntry():
   for i in screen.winfo_children():
      i.destroy()

homeScreen(userName)

screen.mainloop()
PLACING A MATPLOTLIB GRAPH IN A TKINTER WINDOW

from tkinter import *
from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

#Creates the library to hold the data
data = {'Technology': ["Mac","PC","Google Home","Playstation","C64","GoPro","Samsung","iphone","Huawei","X-Box"],
         'User_Rating': [21,45,35,76,34,87,34,5,60,50]}
#Select the variable data for the data to use and then make the
#column equal the technology library index and user rating index
df1 = DataFrame(data,columns=['Technology','User_Rating'])

screen = Tk()
screen.minsize(600,600)
screen.title ("Matplotlib with Tkinter")

myChart = plt.Figure(figsize=(8,5), dpi=100)#Chart size parameters
ax1 = myChart.add_subplot(111)#Grid parameters of the chart
bar1 = FigureCanvasTkAgg(myChart, screen)
bar1.get_tk_widget().pack()
df1 = df1[['Technology','User_Rating']].groupby('Technology').sum()
df1.plot(kind='bar', legend=True, ax=ax1)
ax1.set_title('Technology and Consumer Rating')

screen.mainloop()

LEARN PYTHON CONTENT

Hello World     Download and install Python IDLE and check it works
Data types and variables    Learn how to store values in a variable and work with numbers
Selection and iteration     Learn how to use IF statements and loops.
Functions and procedures     ​Learn how to create functions and procedures
Arrays   Learn how to create and use lists
Formatting Data    Learn how to format numbers and text to suit your needs
Fun with turtle     Complete some fun projects with Python turtle
Object Oriented Programming     Learn how to use OOP principles
Graphic User Interface   Learn how to create a GUI
External File Handling     Learn how to create and manipulate word documents and spreadsheet data
Data Analysis     Learn how to analyse data and present with charts and graphs
SQL with Python     Learn how to link Python with a backend database and manipulate data
Picture
DONATE
Support this site
SUBSCRIBE (FREE)
To enjoy more benefits
We hope you find this site useful. If you notice any errors or would like to contribute material then please contact us.
SUGGESTIONS
We would love to hear from you
SUBSCRIBE 
To enjoy more benefits
We hope you find this site useful. If you notice any errors or would like to contribute material then please contact us.