繼上文用python寫了一個小demo,用控制臺顯示邏輯顯然不是很合理,查詢了很多資料,發現gui編程有tkinter比較好用,然后就在上一個版本的基礎上寫了一個進階
Tkinter是幾個常用 Python GUI 庫之一
現在就讓我們開始把
- 首先我們要匯入 tkinter
from tkinter import * - 然后我們看看怎么使用
root = Tk()
root.mainloop()

- 開始第一步先弄個視窗
# 創建視窗
root = Tk()
# 視窗大小 960x300
root.geometry('960x300')
# 視窗標題
root.title('石頭剪刀布進階')
- 然后先來個標題把,采用的grid柵格布局,比較靈活
# 標題好看成功一半(●'?'●)
# 標簽控制元件 先寫整體的柵格3*4 采用grid布局
col_1 = Label(root, text='%13s' % '猜', font=('華文彩云', 40), fg='purple')
# 標簽定位 row,column不傳默認為0
col_1.grid(row=0, column=0)
col_2 = Label(root, text='%13s' % '拳', font=('華文彩云', 40), fg='purple').grid(row=0, column=1) # 鏈式呼叫
col_3 = Label(root, text='%13s' % '風', font=('華文彩云', 40), fg='purple').grid(row=0, column=2)
col_4 = Label(root, text='%13s' % '云', font=('華文彩云', 40), fg='purple').grid(row=0, column=3)

- next準備用三個按鈕來表示我們的出拳
# 主體應該是三個按鈕
# 提示
tip = Label(root, text='%15s' % '請選擇', font=('行楷', 20), fg='red').grid()
# 按鈕控制元件
button_1 = Button(root, text='石頭', font=('行楷', 20), command=button_click_1, activebackground='pink',
fg='blue').grid(row=1, column=1)
button_2 = Button(root, text='剪刀', font=('行楷', 20), command=button_click_2, activebackground='pink', fg='blue')
button_2.grid(row=1, column=2)
button_3 = Button(root, text=' 布 ', font=('行楷', 20), command=button_click_3, activebackground='pink', fg='blue')
button_3.grid(row=1, column=3)

- 一開始就在想點擊按鈕怎么觸發函式,然后發現了command可以觸發點擊事件
button_1 = Button(root, text='石頭', font=('行楷', 20), command=button_click_1, activebackground='pink',
fg='blue').grid(row=1, column=1)
def button_click_1():
#和電腦猜拳比大小
pass
- 結果提示,本來想直接修改text的值,發現怎么都不行,后面找到StringVar和textvariable可以實作動態渲染
# 結果提示
# 結果值 定義全域
global result
result = StringVar(root)
result_tip = Label(root, textvariable=result, font=('華文彩云', 20), fg='orange').grid(row=2, column=1, columnspan=2)
-
效果圖

-
是不是有點看懵逼了,那就上完整代碼啦哈哈
# -*- coding: utf-8 -*-
# @Time : 2021/02/19 11:30
# @Author : stt
# @Software: PyCharm
""" functions
Gui_demo provides Graphical User Interface for user to make more fun
"""
from tkinter import * # *只匯入初始化的一些并非全部
from tkinter import messagebox # 訊息提示框
import ctypes
import random
# 定義全域變數
global result
global name
name = 'tt'
def create_tk():
""" 表單的主體 """
# 創建視窗
root = Tk()
# 視窗大小 960x300
root.geometry('960x300')
# 視窗標題
root.title('石頭剪刀布進階')
""" 標題 """
# 標題好看成功一半(●'?'●)
# 標簽控制元件 先寫整體的柵格5*5 采用grid布局
col_1 = Label(root, text='%13s' % '猜', font=('華文彩云', 40), fg='purple')
# 標簽定位 row,column不傳默認為0
col_1.grid(row=0, column=0)
col_2 = Label(root, text='%13s' % '拳', font=('華文彩云', 40), fg='purple').grid(row=0, column=1) # 鏈式呼叫
col_3 = Label(root, text='%13s' % '風', font=('華文彩云', 40), fg='purple').grid(row=0, column=2)
col_4 = Label(root, text='%13s' % '云', font=('華文彩云', 40), fg='purple').grid(row=0, column=3)
""" 標題 """
""" 主體 """
# 主體應該是三個按鈕
# 提示
tip = Label(root, text='%15s' % '請選擇', font=('行楷', 20), fg='red').grid()
# 按鈕控制元件
button_1 = Button(root, text='石頭', font=('行楷', 20), command=button_click_1, activebackground='pink',
fg='blue').grid(row=1, column=1)
button_2 = Button(root, text='剪刀', font=('行楷', 20), command=button_click_2, activebackground='pink', fg='blue')
button_2.grid(row=1, column=2)
button_3 = Button(root, text=' 布 ', font=('行楷', 20), command=button_click_3, activebackground='pink', fg='blue')
button_3.grid(row=1, column=3)
# 結果提示
# 結果值 定義全域
# globals()['result'] = StringVar()
global result
result = StringVar(root)
result_tip = Label(root, textvariable=result, font=('華文彩云', 20), fg='orange').grid(row=2, column=1, columnspan=2)
""" 主體 """
# 顯示視窗
root.mainloop()
def button_click_1():
global result
res = game_judge(1)
result.set(res)
def button_click_2():
global result
res = game_judge(2)
result.set(res)
def button_click_3():
global result
res = game_judge(3)
result.set(res)
def game_judge(player_nist):
""" 猜拳邏輯
:param player_nist: 玩家出的拳 1.石頭 2.剪刀 3.布
:return 比賽結果
"""
# 模擬電腦出拳
# TODO 電腦可以設定難度 初步方案可以直接概率勝,或者AI分析
# 這里采用亂數random 1.石頭 2.剪刀 3.布
cpu_nist = random.randint(1, 3)
# 把1 2 3轉化成石頭 剪刀 布 用字典能很好解決這個問題
nist_dict = {1: '石頭', 2: '剪刀', 3: '布'}
# 判斷邏輯 不難發現想贏只有三種 石頭->剪刀,,,即player_nist-cpu_nist==-1 or 2
sub = player_nist - cpu_nist
if sub == -1 or sub == 2:
# 贏了
res = name + '出了' + nist_dict[player_nist] + '\n 電腦出了' + nist_dict[cpu_nist] + "\n恭喜你贏啦(●'?'●)"
elif sub == 0:
# 平局
res = name + '出了' + nist_dict[player_nist] + '\n 電腦出了' + nist_dict[cpu_nist] + '\n平局,有點菜哦o(* ̄▽ ̄*)ブ'
else:
# 輸了
res = name + '出了' + nist_dict[player_nist] + '\n 電腦出了' + nist_dict[cpu_nist] + '\n小菜雞,再去練幾年吧q(≧▽≦q)'
return res
if __name__ == '__main__':
create_tk()
記錄自己成長點滴,也分享給大家學習的心得
(期盼帶給你歡樂😀,祝你學習愉快~)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/262472.html
標籤:其他
