Python Tkinter GUI Tutorials: Calculator, Notepad, and Login/Registration Applications
This article provides step‑by‑step Python Tkinter tutorials for building three desktop GUI applications—a basic calculator, a simple notepad editor, and a user login/registration interface—detailing design principles, UI layout, and complete source code for each example.
This guide demonstrates how to create three practical desktop applications using Python's Tkinter library: a simple arithmetic calculator, a text editor (notepad), and a user login/registration system.
Calculator example introduces the project, explains the required Tkinter components, and shows the UI structure consisting of display, numeric buttons, and operation buttons. The full source code is provided, for instance:
<code>import tkinter
import math
import tkinter.messagebox
class Calculator(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.minsize(280, 450)
self.root.maxsize(280, 470)
self.root.title('计算器')
self.result = tkinter.StringVar()
self.result.set(0)
self.lists = []
self.ispresssign = False
self.menus()
self.layout()
self.root.mainloop()
# ... (additional methods for button actions, calculations, etc.)
</code>The calculator supports basic operations, memory functions, and error handling, illustrating event binding and dynamic UI updates.
Notepad example showcases Tkinter's text widget for creating a lightweight editor. It covers file operations (new, open, save, save‑as), edit functions (cut, copy, paste, undo, redo), and a find dialog. Key code snippets include:
<code>from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
import os
top = Tk()
textPad = Text(top, undo=True)
textPad.pack(expand=YES, fill=BOTH)
def mynew():
top.title('未命名文件')
filename = None
textPad.delete(1.0, END)
def myopen():
filename = askopenfilename(defaultextension='.txt')
if filename:
top.title('记事本' + os.path.basename(filename))
textPad.delete(1.0, END)
with open(filename, 'r') as f:
textPad.insert(1.0, f.read())
</code>This example demonstrates menu creation, keyboard shortcuts, and scrollbar integration.
Login and registration example builds a user authentication interface with entry fields, password masking, and persistent storage using the pickle module. Highlights include:
<code>import tkinter as tk
import pickle
import tkinter.messagebox
window = tk.Tk()
window.title('欢迎登录')
window.geometry('450x300')
var_usr_name = tk.StringVar()
var_usr_name.set('[email protected]')
var_usr_pwd = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=150)
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=190)
def usr_login():
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
try:
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
usrs_info = {'admin': 'admin'}
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(usrs_info, usr_file)
if usr_name in usrs_info and usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo('欢迎光临', usr_name + ':请进入个人首页,查看最新资讯')
else:
tk.messagebox.showinfo(message='错误提示:密码不对,请重试')
</code>The login system includes a registration dialog that validates password confirmation and prevents duplicate usernames.
Overall, the article serves as a hands‑on tutorial for beginners to intermediate Python developers, illustrating Tkinter UI design, event handling, file I/O, and simple data persistence.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.