Python教程 - 石头剪刀布游戏示例

简单的石头剪刀布游戏界面


结果通过弹窗提示:


布局分析

代码示例:

import tkinter
import random
import tkinter.messagebox

def btn_click():
    print('you:',v.get())
    x = random.randint(1, 3)
    print('CPU:',x)
    if x == 1:
        l3.config(text='rock')
        if v.get() == 1:
            tkinter.messagebox.showinfo(message='dual')
        elif v.get() == 2:
            tkinter.messagebox.showinfo(message='you lose')
        else:
            tkinter.messagebox.showinfo(message='you win')
    elif x == 2:
        l3.config(text='scissors')
        if v.get() == 1:
            tkinter.messagebox.showinfo(message='you win')
        elif v.get() == 2:
            tkinter.messagebox.showinfo(message='dual')
        else:
            tkinter.messagebox.showinfo(message='you lose')
    else:
        l3.config(text='paper')
        if v.get() == 1:
            tkinter.messagebox.showinfo(message='you lose')
        elif v.get() == 2:
            tkinter.messagebox.showinfo(message='you win')
        else:
            tkinter.messagebox.showinfo(message='dual')

main = tkinter.Tk()
main.geometry('300x100')

l1 = tkinter.Label(main, text='You:')
l1.grid(row=0, column=0)

v = tkinter.IntVar()

r1 = tkinter.Radiobutton(main, text='rock', variable=v, value=1)
r1.grid(row=0, column=1)

r2 = tkinter.Radiobutton(main, text='scissors', variable=v, value=2)
r2.grid(row=0, column=2)

r3 = tkinter.Radiobutton(main, text='paper', variable=v, value=3)
r3.grid(row=0, column=3)

b1 = tkinter.Button(main, text='OK')
b1.config(command=btn_click)
b1.grid(row=0, column=4)

l2 = tkinter.Label(main, text='CPU:')
l2.grid(row=1, column=0)

l3 = tkinter.Label(main, text='Waiting')
l3.grid(row=1, column=1)

给界面添加上图片



完整代码

import tkinter
import random
import tkinter.messagebox

def btn_click():
    print('you:',v.get())
    x = random.randint(1, 3)
    print('CPU:',x)
    if x == 1:
        l3.config(text='rock', image=p1)
        if v.get() == 1:
            tkinter.messagebox.showinfo(message='dual')
        elif v.get() == 2:
            tkinter.messagebox.showinfo(message='you lose')
        else:
            tkinter.messagebox.showinfo(message='you win')
    elif x == 2:
        l3.config(text='scissors', image=p2)
        if v.get() == 1:
            tkinter.messagebox.showinfo(message='you win')
        elif v.get() == 2:
            tkinter.messagebox.showinfo(message='dual')
        else:
            tkinter.messagebox.showinfo(message='you lose')
    else:
        l3.config(text='paper', image=p3)
        if v.get() == 1:
            tkinter.messagebox.showinfo(message='you lose')
        elif v.get() == 2:
            tkinter.messagebox.showinfo(message='you win')
        else:
            tkinter.messagebox.showinfo(message='dual')

main = tkinter.Tk()
main.geometry('300x100')

p1 = tkinter.PhotoImage(file='shitou.png')
p2 = tkinter.PhotoImage(file='jiandao.png')
p3 = tkinter.PhotoImage(file='bu.png')

l1 = tkinter.Label(main, text='You:')
l1.grid(row=0, column=0)

v = tkinter.IntVar()

r1 = tkinter.Radiobutton(main, text='rock', variable=v, value=1, image=p1)
r1.grid(row=0, column=1)

r2 = tkinter.Radiobutton(main, text='scissors', variable=v, value=2, image=p2)
r2.grid(row=0, column=2)

r3 = tkinter.Radiobutton(main, text='paper', variable=v, value=3, image=p3)
r3.grid(row=0, column=3)

b1 = tkinter.Button(main, text='OK')
b1.config(command=btn_click)
b1.grid(row=0, column=4)

l2 = tkinter.Label(main, text='CPU:')
l2.grid(row=1, column=0)

l3 = tkinter.Label(main, text='Waiting')
l3.grid(row=1, column=1)

posted @ 2022-06-05 19:01  hgrun  阅读(124)  评论(0编辑  收藏  举报