python tkinter 出牌洗牌
利用Tkinter模块设计出扑克牌发牌窗体程序,要求4名牌手打牌,计算机随机将52张牌(不含大小王)发给4名牌手,在屏幕上显示每位牌手的牌。程序运行结果如图所示。
- 本程序只是实现一个界面和出牌洗牌的功能,不是斗地主游戏!!!
- 有两个问题没有解决:洗牌的动画效果不好
- 每次出牌后不能把新打出的牌放到最顶层
源代码:
from functools import partial
from tkinter import *
import random
import time
from PIL import Image,ImageTk
tk=Tk()
tk.title("洗牌,出牌")
tk.geometry("800x600+250+30")
tk.resizable(0,0)
bu=Button()
buttons=[bu]*52
delbuttons=[]
sign=[]
imgs=[]
for i in range(1,5):
for j in range(1,14):
imgs.insert((i-1)*13+(j-1),PhotoImage(file='images\\'+str(i)+'-'+str(j)+'.gif'))
can=Canvas(tk,width=800,height=600)
im=Image.open('images\\bg.gif')
img=ImageTk.PhotoImage(im.resize((800,600)))
can.create_image(400,300,image=img)
can.pack()
def delbutton():
for i in range(0,52):
buttons[i]=bu
buttons[i].place_forget()
for i in range(0,len(delbuttons)):
delbuttons[i].place_forget()
tk.after(500,washcard1)
def washcard1():
random.shuffle(imgs)
a=230
for i in range(0,13):
l1=Button(tk,image=imgs[i],compound=CENTER)
command = partial(downclik, l1,a,i)
l1.config(command=command)
l1.place(x=a,y=60)
a+=20
delbuttons.append(l1)
a=100
for i in range(13,26):
l1=Button(tk,image=imgs[i],compound=CENTER)
command = partial(leftclik, l1,a,i)
l1.config(command=command)
l1.place(x=630,y=a)
a+=20
delbuttons.append(l1)
a=230
for i in range(26,39):
l1=Button(tk,image=imgs[i],compound=CENTER)
command = partial(upclik, l1,a,i)
l1.config(command=command)
l1.place(x=a,y=400)
a+=20
delbuttons.append(l1)
a=100
for i in range(39,52):
l1=Button(tk,image=imgs[i],compound=CENTER)
command = partial(rightclik, l1,a,i)
l1.config(command=command)
l1.place(x=70,y=a)
a+=20
delbuttons.append(l1)
def downclik(button,a,i):
if buttons[i]==bu:
button.place(x=a,y=80)
buttons[i]=button
else:
button.place(x=a,y=60)
buttons[i]=bu
def leftclik(button,a,i):
if buttons[i]==bu:
button.place(x=600,y=a)
buttons[i]=button
else:
button.place(x=630,y=a)
buttons[i]=bu
def upclik(button,a,i):
if buttons[i]==bu:
button.place(x=a,y=380)
buttons[i]=button
else:
button.place(x=a,y=400)
buttons[i]=bu
def rightclik(button,a,i):
if buttons[i]==bu:
button.place(x=100,y=a)
buttons[i]=button
else:
button.place(x=70,y=a)
buttons[i]=bu
def upcard():
'''
for j in range(0,len(sign)):
#delbuttons[sign[j]].place_forget()
buttons[sign[j]].place_forget()
sign.pop()
print("hello")
'''
a=300
for i in range(0,52):
if buttons[i]!=bu:
buttons[i].place(x=a,y=225)
a+=10
def washcard():
tk.after(100,delbutton)
def playcard():
button=Button(tk,text='出牌',bd=5,bg='red',command=upcard,width=10,height=2)
button.place(x=480,y=270)
def washbutton():
button=Button(tk,text='洗牌',bd=5,bg='red',command=washcard,width=10,height=2)
button.place(x=200,y=270)
washbutton()
washcard()
playcard()
tk.mainloop()
|