@ the Author: Runsen

Very few people use Tkinter now, so even fewer people actually study it, and didn’t want to update tkinter. I see a lot of people learning Tkinter, actually doing layout in Python, nobody does that. But update a few sections of Tkinter in Python from getting started to master tutorials.

tkinter

The Tkinter package is a standard package that comes with Python, so we don’t need to install anything to use it.

Window body frame

The body framework of every Tkinter application can contain the following section. Define the window and some properties of the window, then write the window contents, and finally execute window.mainloop to make the window come alive.

import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('200x100')

# Here is the contents of the window
window.mainloop()

Copy the code

Window contents

This time we will create a Label to describe tk.label, such as:

import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('200x100')



l = tk.Label(window, 
    text='OMG! this is TK! '.# tag text
    bg='green'.# Background color
    font=('Arial'.12),     # font and font size
    width=15, height=2  Length and width of the tag
    )
l.pack()    # Fix window position

window.mainloop()

Copy the code

controls

The Label above is a control, and there are many more, such as buttons, labels and text boxes, as shown in the figure below

Control comes with common properties, such as size, font, and color. You can select the corresponding properties according to the display form of the control. The specific properties are listed as follows:

Tkinter binding event

Tkinter binding event: define a function and pass in the function name via the command attribute


from tkinter import *

def p_label() :
    global root
    Lb = Label(root, text='Runsen loves to learn ')
    Lb.pack()

root = Tk()
root.title("Application Window")
B_n = Button(root, text='am I', command=p_label, bg='red')  # command must not have any punctuation marks after it
B_n.pack()
root.mainloop()
Copy the code

The layout shows

A window should have a layout, that is, when pack needs to set side, expand needs to expand, fill needs to be filled

from tkinter import *
root = Tk()
root.title("Application Window")
Button(root,text='1').pack(side=LEFT,expand=YES,fill=Y)
Button(root,text='2').pack(side=TOP,expand=YES,fill=BOTH)
Button(root,text='3').pack(side=RIGHT,expand=YES,fill=NONE)
Button(root,text='4').pack(side=LEFT,expand=NO,fill=Y)
Button(root,text='5').pack(side=TOP,expand=YES,fill=BOTH)
Button(root,text='6').pack(side=BOTTOM,expand=YES)
Button(root,text='7').pack(anchor=SE)
root.mainloop()
Copy the code

In addition to pack, there is a grid, which lays out components as tables

Let’s make a phone dial GUI

from tkinter import *
root = Tk()
labels = [['1'.'2'.'3'].# Text, layout as grid
          ['4'.'5'.'6'],
          ['7'.'8'.'9'],
          [The '*'.'0'.The '#']]

for r in range(4) :# line cycle
    for c in range(3) :# column loop
        label = Label(root,
                      relief=RAISED, # Set the border format
                      padx=10.# widen the tag
                      text=labels[r][c]) # tag text
        label.grid(row=r, column=c) # place the tag on row R, column C
root.mainloop()
Copy the code

Make a Calendar

Above teach you to make a phone dial GUI, below can make a simple calendar?

I don’t think you would. Not that I despise you

Don’t worry, I’m here for you. This requires importing the Calendar module,

import calendar
from tkinter import *
root = Tk()
labels = [['Mon'.'Tue'.'Wed'.'Thu'.'Fri'.'Sat'.'Sun']]

MonthCal = calendar.monthcalendar(2020.5) 
for i in range(len(MonthCal)):
    labels.append(MonthCal[i])
for r in range(len(MonthCal)+1) :for c in range(7) :if labels[r][c] == 0:
            labels[r][c] = ' '
        label = Label(root,          
                      padx=5,
                      pady=5,
                      text=str(labels[r][c]))        
        label.grid(row=r,column=c)
root.mainloop()
Copy the code

Enrich our calendar

The calendar above is a hot chicken, what functions are not, the demand is very simple, is to two buttons to achieve up, down.

Scroll up and scroll down two buttons to clear the interface, and then add the calendar to the Labels list to place the calendar. It seems simple, but it is.

So let’s think about how we could do that. I’ll do the implementation code for the standard

# @ Author: Runsen
import calendar 
from tkinter import *
root = Tk()


def LabelCal(Year, Month) :
    # Place "year, month" in the first line
    label = Label(root,text=str(Year)+"Year")
    label.grid(row=0,column=2)
    label = Label(root,text=str(Month)+"Month")
    label.grid(row=0,column=4)
    # Labels list: Place "week" headings
    labels = [['Mon'.'Tue'.'Wed'.'Thu'.'Fri'.'Sat'.'Sun']]
    Use calendar library to calculate calendar
    MonthCal = calendar.monthcalendar(Year, Month)
    Clear the interface first
    for r in range(7) :for c in range(7):            
            label = Label(root,
                          width =5,
                          padx=5,
                          pady=5,
                          text=' ')        
            label.grid(row=r+1,column=c)
    # Add calendar to labels list
    for i in range(len(MonthCal)):
        labels.append(MonthCal[i])
    # Place calendar
    for r in range(len(MonthCal)+1) :for c in range(7) :if labels[r][c] == 0:
                labels[r][c] = ' '
            label = Label(root,
                          width =5,
                          padx=5,
                          pady=5,
                          text=str(labels[r][c]))        
            label.grid(row=r+1,column=c) # Grid layout


# default date
Year, Month = 2020.5
LabelCal(Year, Month)
        
# button: Enter
def ButtonPrevious() :
    global Year, Month
    Month = Month-1
    if Month<1:
        Month = Month+12
        Year = Year-1
    LabelCal(Year, Month)
    
button1 = Button(root, text='Previous', command=ButtonPrevious)
button1.grid(row=len(MonthCal)+3, column=0)


# button: the Clear
def ButtonNext() :
    global Year, Month
    Month = Month+1
    if Month>12:
        Month = Month-12
        Year = Year+1 
    LabelCal(Year, Month)
    
button2 = Button(root, text='Next', command=ButtonNext)
button2.grid(row=len(MonthCal)+3, column=6)

root.mainloop()
Copy the code

Run a wave to get a final GIF effect.

GitHub, portal ~, GitHub, portal ~, GitHub, Portal ~