当前位置:   article > 正文

python tkinter random messagebox 实现一个界面化的石头剪刀布!_python实现界面化

python实现界面化

tkinter 实现一个界面化的石头剪刀布

今天上网看了一下,网上的tkinter做的石头剪刀布要么没有图片、音乐资源,要么代码配备不完整**(没有批评的意思)**。所以,我写了一个不需要图片、音乐文件,富有完整代码的石头剪刀布小游戏。(完整代码可以在文末复制)

定义函数main

def main():
  • 1

1.界面及其一些设置

	root=Tk()
	root.title('石头剪刀布')
	root.geometry('+180+100')
	root.overrideredirect(True)
  • 1
  • 2
  • 3
  • 4

2.创建6个函数,下面使用

	def tuichu():
        if askyesno('退出','是否要退出?'):root.destroy()
    def print(s):
        showinfo('提示',s)
    def shitou():
        print('你出石头')
        return shitoujiandaobu('shitou')
    def jiandao():
        print('你出剪刀')
        return shitoujiandaobu('jiandao')
    def bu():
        print('你  出  布')
        return shitoujiandaobu('bu')
    def shitoujiandaobu(chu):
        dichu=randint(1,3)
        if dichu==1:di='shitou';print('电脑出石头')
        elif dichu==2:di='jiandao';print('电脑出剪刀')
        else:di='bu';print('电 脑 出 布')
        if chu==di:
            print('平局!')
        elif (chu=='jiandao'and di=='bu')or(chu=='bu'and di=='shitou')or(chu=='shitou'and di=='jiandao'):
            print('你赢了!')
        else:print('你输了!')              
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

到这儿,大家应该发现了,我改写了print函数,大家可以把改写print函数与下文import的tkinter.messagebox.showinfo删掉

3.创建按钮

    Button(root, text='石头',command=shitou,bg='yellow',fg='red',font=('微软雅黑',38)).pack()
    Button(root, text='剪刀',command=jiandao,fg='red',font=('微软雅黑',38)).pack()
    Button(root, text=' 布  ',command=bu,bg='yellow',fg='red',font=('微软雅黑',40)).pack()
    Button(root, text='退出',command=tuichu,bg='green',fg='purple',font=('微软雅黑',38)).pack()
  • 1
  • 2
  • 3
  • 4

4.mainloop

    root.mainloop()
  • 1

5.导入 、 运行

if __name__=='__main__':
    from tkinter import Tk,Button
    from random import randint
    from tkinter.messagebox import showinfo,askyesno
    main()
  • 1
  • 2
  • 3
  • 4
  • 5

完整代码

def main():
    root=Tk()
    root.title('石头剪刀布');root.geometry('+180+100');root.overrideredirect(True)
    def tuichu():
        if askyesno('退出','是否要退出?'):root.destroy()
    def print(s):
        showinfo('提示',s)
    def shitou():
        print('你出石头')
        return shitoujiandaobu('shitou')
    def jiandao():
        print('你出剪刀')
        return shitoujiandaobu('jiandao')
    def bu():
        print('你  出  布')
        return shitoujiandaobu('bu')
    def shitoujiandaobu(chu):
        dichu=randint(1,3)
        if dichu==1:di='shitou';print('电脑出石头')
        elif dichu==2:di='jiandao';print('电脑出剪刀')
        else:di='bu';print('电 脑 出 布')
        if chu==di:
            print('平局!')
        elif (chu=='jiandao'and di=='bu')or(chu=='bu'and di=='shitou')or(chu=='shitou'and di=='jiandao'):
            print('你赢了!')
        else:print('你输了!')
    Button(root, text='石头',command=shitou,bg='yellow',fg='red',font=('微软雅黑',38)).pack()
    Button(root, text='剪刀',command=jiandao,fg='red',font=('微软雅黑',38)).pack()
    Button(root, text=' 布  ',command=bu,bg='yellow',fg='red',font=('微软雅黑',40)).pack()
    Button(root, text='退出',command=tuichu,bg='green',fg='purple',font=('微软雅黑',38)).pack()
    root.mainloop()
if __name__=='__main__':
    from tkinter import Tk,Button
    from random import randint
    from tkinter.messagebox import showinfo,askyesno
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/230040
推荐阅读
相关标签