Practice: place管理器-扑克牌页面设计

 1 # coding:utf-8
 2 from tkinter import *
 3 from tkinter import messagebox
 4 import random
 5 
 6 
 7 class Application(Frame):
 8     """一个经典的GUI程序类写法"""
 9     def __init__(self, master=None):
10         super().__init__(master)          # super代表的是父类的定义,而不是父类的对象
11         self.master = master
12         self.pack()
13         self.createWidget()
14 
15     def createWidget(self):
16         self.photos = [PhotoImage(file='1/pic'+str(i+1)+'.gif')for i in range(5)]
17         self.pukes = [Label(self.master, image=self.photos[i])for i in range(5)]
18 
19         for i in range(5):
20             self.pukes[i].place(x=10+i*100, y=50)
21 
22         # 为所有labels加事件处理
23         self.pukes[0].bind_class('Label', '<Button-1>', self.chupai)
24 
25     def chupai(self, event):
26         if event.widget.winfo_y() == 50:
27             event.widget.place(y=30)
28         if event.widget.winfo_y() == 30:
29             event.widget.place(y=50)
30 
31 if __name__ == "__main__":
32     root = Tk()
33     root.geometry("800x600+200+100")
34     root.title('canvas')
35     app = Application(master=root)
36     root.mainloop()