Python Chapter 9: 使用Tkinter进行GUI程序设计 Exercise
Exercise 9_1:
1 # Exercise 9.1 by TBNR_Gabriel 2 from tkinter import * 3 4 class movingBall: 5 def __init__(self): 6 window = Tk() 7 window.title("Moving Ball") 8 9 self.x = 100 10 self.y = 50 11 self.radius = 3 12 13 frame1 = Frame() 14 frame1.pack() 15 self.canvas = Canvas(frame1, width = 200, height = 100, bg = "white") 16 self.canvas.pack() 17 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, fill = "red", tags = "oval") 18 19 frame2 = Frame() 20 frame2.pack() 21 btRight = Button(frame2, text = "Right", command = self.right) 22 btRight.pack(side = LEFT) 23 btLeft = Button(frame2, text = "Left", command = self.left) 24 btLeft.pack(side = LEFT) 25 btUp = Button(frame2, text = "Up", command = self.up) 26 btUp.pack(side = LEFT) 27 btDown = Button(frame2, text = "Down", command = self.down) 28 btDown.pack(side = LEFT) 29 30 window.mainloop() 31 32 def right(self): 33 if self.x <= 190: 34 self.x += 10 35 elif self.x == 200: 36 self.x = 0 37 self.canvas.delete("oval") 38 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, fill = "red", tags = "oval") 39 40 def left(self): 41 if self.x >= 10: 42 self.x -= 10 43 elif self.x == 0: 44 self.x = 200 45 self.canvas.delete("oval") 46 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, fill = "red", tags = "oval") 47 48 def up(self): 49 if self.y >= 10: 50 self.y -= 10 51 elif self.y == 0: 52 self.y = 100 53 self.canvas.delete("oval") 54 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, fill = "red", tags = "oval") 55 56 def down(self): 57 if self.y <= 90: 58 self.y += 10 59 elif self.y == 100: 60 self.y = 0 61 self.canvas.delete("oval") 62 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, fill = "red", tags = "oval") 63 64 movingBall()
Exercise 9_19:
1 # Exercise 9.19 by TBNR_Gabriel 2 from tkinter import * 3 4 class movingBall2: 5 def __init__(self): 6 window = Tk() 7 window.title("Moving Ball") 8 9 self.width = 1200 10 self.height = 700 11 12 self.canvas = Canvas(window, width = self.width, height = self.height, bg = "blue") 13 self.canvas.pack() 14 15 self.x = self.width / 2 16 self.y = self.height 17 self.radius = self.height / 14 18 19 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, tags = "oval", fill = "yellow") 20 21 self.canvas.bind("<Key>", self.process) 22 self.canvas.focus_set() 23 window.mainloop() 24 25 def process(self, event): 26 if event.keysym == "w": 27 self.up() 28 elif event.keysym == "a": 29 self.left() 30 elif event.keysym == "s": 31 self.down() 32 elif event.keysym == "d": 33 self.right() 34 if self.y == 100: 35 self.canvas.create_text(800, 100, text = "海上升明月,coding共此时", font = "Times 16 bold", tags = "string") 36 self.canvas.create_text(815, 150, text = "中秋快乐", font = "Times 16 bold", tags = "string") 37 38 def up(self): 39 if self.y >= 10: 40 self.y -= 10 41 elif self.y == 0: 42 self.y = self.height 43 self.canvas.delete("oval") 44 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, tags = "oval", fill = "yellow") 45 46 def down(self): 47 if self.y <= self.height - 10: 48 self.y += 10 49 elif self.y == self.height: 50 self.y = 0 51 self.canvas.delete("oval") 52 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, tags = "oval", fill = "yellow") 53 54 def left(self): 55 if self.x >= 10: 56 self.x -= 10 57 elif self.x == 0: 58 self.x = self.width 59 self.canvas.delete("oval") 60 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, tags = "oval", fill = "yellow") 61 62 def right(self): 63 if self.x <= self.width - 10: 64 self.x += 10 65 elif self.x == self.width: 66 self.x = 0 67 self.canvas.delete("oval") 68 self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, tags = "oval", fill = "yellow") 69 70 movingBall2()
Exercise 9.25:
1 # Exercise 9.25 by TBNR_Gabriel 2 from tkinter import * 3 4 class trafficLight: 5 def __init__(self): 6 window = Tk() 7 window.title("Traffic Light") 8 9 frame1 = Frame(window) 10 frame1.pack() 11 self.canvas = Canvas(frame1, width = 100, height = 200, bg = "white") 12 self.canvas.pack() 13 self.canvas.create_oval(20, 10, 80, 70, tags = "oval1") 14 self.canvas.create_oval(20, 70, 80, 130, tags = "oval2") 15 self.canvas.create_oval(20, 130, 80, 190, tags = "oval3") 16 17 frame2 = Frame(window) 18 frame2.pack() 19 self.v1 = IntVar() 20 rbRed = Radiobutton(frame2, text = "Red", bg = "red", variable = self.v1, value = 1, command = self.processRadiobutton) 21 rbRed.pack(side = LEFT) 22 rbYellow = Radiobutton(frame2, text = "Yellow", bg = "yellow", variable = self.v1, value = 2, command = self.processRadiobutton) 23 rbYellow.pack(side = LEFT) 24 rbGreen = Radiobutton(frame2, text = "Green", bg = "green", variable = self.v1, value = 3, command = self.processRadiobutton) 25 rbGreen.pack(side = LEFT) 26 27 def processRadiobutton(self): 28 if self.v1.get() == 1: 29 self.canvas.delete("oval1", "oval2", "oval3") 30 self.canvas.create_oval(20, 10, 80, 70, tags = "oval1", fill = "red") 31 self.canvas.create_oval(20, 70, 80, 130, tags = "oval2") 32 self.canvas.create_oval(20, 130, 80, 190, tags = "oval3") 33 elif self.v1.get() == 2: 34 self.canvas.delete("oval1", "oval2", "oval3") 35 self.canvas.create_oval(20, 10, 80, 70, tags = "oval1") 36 self.canvas.create_oval(20, 70, 80, 130, tags = "oval2", fill = "yellow") 37 self.canvas.create_oval(20, 130, 80, 190, tags = "oval3") 38 elif self.v1.get() == 3: 39 self.canvas.delete("oval1", "oval2", "oval3") 40 self.canvas.create_oval(20, 10, 80, 70, tags = "oval1") 41 self.canvas.create_oval(20, 70, 80, 130, tags = "oval2") 42 self.canvas.create_oval(20, 130, 80, 190, tags = "oval3", fill = "green") 43 44 trafficLight()
本人为西安交通大学电信学院本科生,欢迎对相关内容提出批评与建议!
邮箱:fsbmoocs@163.com