1、一个经典的面向程序写法模块
from tkinter import *
from tkinter import messagebox
class Appication(Frame):
"""
一个经典的 GUI 类的写法
"""
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.pack()
self.CreateWidget()
def CreateWidget(self):
"""
用途:通过 Place 布局管理器实现扑克牌位置控制
"""
......
......
if __name__ == "__main__":
window = Tk()
window.geometry("570x200+200+200")
window.title('扑克牌界面')
app = Appication(master = window)
window.mainloop()
2、布局管理器代码(place)
def CreateWidget(self):
"""
用途:通过 Place 布局管理器实现扑克牌位置控制
"""
self.photos = [PhotoImage(file = "image\\puke"+str(i + 1)+".gif") for i in range(10)]
self.pukes = [Label(self.master, image = self.photos[i]) for i in range(10)]
for i in range(10):
self.pukes[i].place(x = 10 + i * 40, y = 50)
self.pukes[0].bind_class("Label", "<Button-1>", self.chupai)
3、事件操作模块
def chupai(self, event):
print(event.widget.winfo_geometry())
print(event.widget.winfo_x())
if event.widget.winfo_y() == 30:
event.widget.place(y = 50)
if event.widget.winfo_y() == 50:
event.widget.place(y = 30)
4、源代码
from tkinter import *
from tkinter import messagebox
class Appication(Frame):
"""
一个经典的 GUI 类的写法
"""
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.pack()
self.CreateWidget()
def CreateWidget(self):
"""
用途:通过 Place 布局管理器实现扑克牌位置控制
"""
self.photos = [PhotoImage(file = "image\\puke"+str(i + 1)+".gif") for i in range(10)]
self.pukes = [Label(self.master, image = self.photos[i]) for i in range(10)]
for i in range(10):
self.pukes[i].place(x = 10 + i * 40, y = 50)
self.pukes[0].bind_class("Label", "<Button-1>", self.chupai)
def chupai(self, event):
print(event.widget.winfo_geometry())
print(event.widget.winfo_x())
if event.widget.winfo_y() == 30:
event.widget.place(y = 50)
if event.widget.winfo_y() == 50:
event.widget.place(y = 30)
if __name__ == "__main__":
window = Tk()
window.geometry("570x200+200+200")
window.title('扑克牌界面')
app = Appication(master = window)
window.mainloop()