本系列博文基于廖雪峰老师的官网Python教程,笔者在大学期间已经阅读过廖老师的Python教程,教程相当不错,官网链接: 廖雪峰官方网站.请需要系统学习Python的小伙伴到廖老师官网学习,笔者的编程环境是Anaconda+Pycharm,Python版本:Python3.
# Tkinter # 1.Python调用内置的Tkinter,Tkinter封装了访问Tk的接口; # 2.Tk是一个图像库,支持多个操作系统,使用Tcl语言开发; # 3.Tk会调用操作系统提供本地GUI接口,完成最终的GUI; from tkinter import * class Application(Frame): def __init__(self, master = None): Frame.__init__(self, master) self.pack() self.createWidgets() def createWidgets(self): self.welcomeLabel = Label(self, text = "Welcome to FUXI Technology.") self.welcomeLabel.pack() self.quitButton = Button(self, text = "Quit", command = self.quit) self.quitButton.pack() app = Application() app.master.title("Welcome") # 设置窗口标题 app.mainloop() # 主消息循环
# 输入文本 from tkinter import * import tkinter.messagebox as messagebox class Application(Frame): def __init__(self, master = None): Frame.__init__(self, master) self.pack() self.createWidgets() def createWidgets(self): self.nameInput = Entry(self) self.nameInput.pack() self.alertButton = Button(self, text = "Welcome", command = self.welcome) self.alertButton.pack() def welcome(self): name = self.nameInput.get() or "to FUXI Technology." messagebox.showinfo("Message", "Welcome %s" % name) app = Application() app.master.title("Welcome") app.mainloop()
# 1.绘制长方形 from turtle import * width(3) # 设置笔刷宽度 forward(168) # 前进 right(90) # 右转90° pencolor("red") # 笔刷颜色 forward(100) right(90) pencolor("green") forward(168) right(90) pencolor("blue") forward(100) right(90) done()
# 2.绘制5个五角星 from turtle import * def drawStar(x, y): penup() goto(x, y) pd() seth(0) for i in range(5): fd(40) rt(144) for x in range(0, 250, 50): drawStar(x, 0) done()
# 3.绘制分型树 from turtle import * colormode(255) lt(90) lv = 14 l = 120 s = 45 width(lv) r = 0 g = 0 b = 0 pencolor(r, g, b) penup() bk(l) pendown() fd(l) def draw_tree(l, level): global r, g, b # save the current pen width w = width() # narrow the pen width width(w * 3.0 / 4.0) # set color: r = r + 1 g = g + 2 b = b + 3 pencolor(r % 200, g % 200, b % 200) l = 3.0 / 4.0 * l lt(s) fd(l) if level < lv: draw_tree(l, level + 1) bk(l) rt(2 * s) fd(l) if level < lv: draw_tree(l, level + 1) bk(l) lt(s) # restore the previous pen width width(w) speed("fastest") draw_tree(l, 4) done()