好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > python tkinter 做個簡單的計算器的方法

python tkinter 做個簡單的計算器的方法

熱門標簽:蘇州人工外呼系統軟件 佛山通用400電話申請 京華圖書館地圖標注 看懂地圖標注方法 打印谷歌地圖標注 淮安呼叫中心外呼系統如何 電話機器人貸款詐騙 廣東旅游地圖標注 電話外呼系統招商代理

背景

最近本菜雞在學習 python GUI,從 tkinter 入門,想先做個小軟件練習一下
思來想去,決定做一個 計算器

設計思路

首先,導入我們需要的包 — tkinter,并通過 實例化一個 Tk 對象 創建窗口
因為我有點菜,目前還把控不好各組件的位置,所以窗口使用自動默認的大小

import tkinter as tk
import tkinter.messagebox
win = tkinter.Tk()
win.title("計算器")
win.mainloop()

大致 規劃 各組件的 位置

我的目標是做成這個樣子(最終效果)

大致規劃好位置后,我創建了 四個 Frame,如下
u1s1,感覺兩三個就夠了

# 承載提示信息與輸入框的框架
entry_frame = tk.Frame(win)
# 承載運算符號的框架
menu_frame = tk.Frame(win)
# 承載數字的框架
major_frame = tk.Frame(win)
# 承載等號的框架
cal_frame = tk.Frame(win)

entry_frame.pack(side="top")
menu_frame.pack(side="left")
major_frame.pack()
cal_frame.pack(side="right")

下面就做一個 輸入框,分為兩部分

  • 一部分是漢字部分,提示信息,使用 Label 控件
  • 一部分是輸入框,使用 Entry 控件
t_label = tk.Label(entry_frame, text = "請輸入 : ")
t_label.pack(side='left')
word_entry = tk.Entry(
    entry_frame,
    fg = "blue", # 輸入字體顏色,設置為藍色
    bd = 3, # 邊框寬度
    width = 39, # 輸入框長度
    justify = 'right' # 設置對齊方式為靠右
)
word_entry.pack()

然后在下面的左側 排列運算符號

for char in ['+', '-', '×', '÷']:
    myButton(menu_frame, char, word_entry)

其中,myButton 類實例化一個按鈕,并且當點擊按鈕時,輸入框會出現相應的文本
當時遇到了問題 — 點擊按鈕無法獲得爭取的按鈕上的文本你, 解決后寫了一篇博客,傳送門

用相同的辦法 列舉各個數字

for i in range(4):
    num_frame = tk.Frame(major_frame)
    num_frame.pack()
    if i  3:
        for count in range(3*i+1, 3*i+4):
            myButton(num_frame, count, word_entry, side=(i, count))
        continue
    myButton(num_frame, 0, word_entry, side=(i, 0))

當然,重置按鈕和計算按鈕 可不能忘
最后的計算就懶了一點,直接使用 entry.get() 獲得要計算的式子,使用 eval() 函數計算,如果格式錯誤即彈窗提示

def calculate(entry):
    try:
        result = entry.get()
        # 如果輸入框中不存在字符串,則 = 按鈕不管用
        if result == '':
            return
        result = eval(result)
        entry.delete(0, "end")
        entry.insert(0, str(result))
    except:
        tkinter.messagebox.showerror("錯誤", "格式錯誤!\n請重新輸入!")
reset_btn = tk.Button(
    cal_frame,
    text = '重置',
    activeforeground = "blue",
    activebackground = "pink",
    width = "13",
    command = lambda :word_entry.delete(0, "end")
).pack(side="left")
result_btn = tk.Button(
    cal_frame,
    text = '=',
    activeforeground = "blue",
    activebackground = "pink",
    width = "13",
    command = lambda :calculate(word_entry)
).pack(side="right")

全部代碼

major.py

# -*- coding=utf-8 -*-
# @Time    : 2021/3/4 13:06
# @Author  : lhys
# @FileName: major.py

myName = r'''
    Welcome, my master!
    My Name is :
     ____                ____        ____        ____         ____              ______________
    |    |              |    |      |    |      |    \       /    |           /              /
    |    |              |    |      |    |      |     \     /     |          /              /
    |    |              |    |      |    |      |      \   /      |         /              /
    |    |              |    |      |    |       \      \_/      /         /       _______/
    |    |              |    |______|    |        \             /          \            \

    |    |              |                |         \           /            \            \

    |    |              |     ______     |          \         /              \            \

    |    |              |    |      |    |           \       /                \________    \

    |    |              |    |      |    |            |     |               /              /
    |    |_______       |    |      |    |            |     |              /              /
    |            |      |    |      |    |            |     |             /              /
    |____________|      |____|      |____|            |_____|            /______________/
    '''
print(myName)
import tkinter as tk
from tools import *

win = tk.Tk()
win.title('計算器')

entry_frame = tk.Frame(win)
menu_frame = tk.Frame(win)
major_frame = tk.Frame(win)
cal_frame = tk.Frame(win)

entry_frame.pack(side="top")
menu_frame.pack(side="left")
major_frame.pack()
cal_frame.pack()

# 輸入框
t_label = tk.Label(entry_frame, text = "請輸入 : ")
t_label.pack(side='left')
word_entry = tk.Entry(
    entry_frame,
    fg = "blue",
    bd = 3,
    width = 39,
    justify = 'right'
)
word_entry.pack()


# 菜單欄
for char in ['+', '-', '×', '÷']:
    myButton(menu_frame, char, word_entry)

button_side = ['right', 'left']

for i in range(4):
    num_frame = tk.Frame(major_frame)
    num_frame.pack()
    if i  3:
        for count in range(3*i+1, 3*i+4):
            myButton(num_frame, count, word_entry, side=(i, count))
        continue
    myButton(num_frame, 0, word_entry, side=(i, 0))

reset_btn = tk.Button(
    cal_frame,
    text = '重置',
    activeforeground = "blue",
    activebackground = "pink",
    width = "13",
    command = lambda :word_entry.delete(0, "end")
).pack(side="left")
result_btn = tk.Button(
    cal_frame,
    text = '=',
    activeforeground = "blue",
    activebackground = "pink",
    width = "13",
    command = lambda :calculate(word_entry)
).pack(side="right")

win.mainloop()

tools.py

# -*- coding=utf-8 -*-
# @Time    : 2021/3/4 13:20
# @Author  : lhys
# @FileName: tools.py

import tkinter
import tkinter.messagebox

def calculate(entry):
    try:
        result = entry.get()
        if result == '':
            return
        result = eval(result)
        print(result)
        entry.delete(0, "end")
        entry.insert(0, str(result))
    except:
        tkinter.messagebox.showerror("錯誤", "格式錯誤!\n請重新輸入!")

class myButton():
    def __init__(self, frame, text, entry, **kwargs):
        side = kwargs.get('side') if 'side' in kwargs else ()
        self.btn = tkinter.Button(
            frame,
            text = text,
            activeforeground="blue",
            activebackground="pink",
            width="13",
            command=lambda :entry.insert("end", text)
        )
        if side:
            self.btn.grid(row=side[0], column=side[1])
        else:
            self.btn.pack()

到此這篇關于python tkinter 做個簡單的計算器的方法的文章就介紹到這了,更多相關python tkinter 計算器內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 利用Tkinter(python3.6)實現一個簡單計算器
  • 基于python的Tkinter實現一個簡易計算器
  • Python+tkinter使用80行代碼實現一個計算器實例
  • Python編程使用tkinter模塊實現計算器軟件完整代碼示例
  • Python Tkinter實現簡易計算器功能
  • python使用tkinter實現簡單計算器
  • Python tkinter實現簡單加法計算器代碼實例
  • Python+tkinter使用40行代碼實現計算器功能
  • 如何利用python的tkinter實現一個簡單的計算器

標簽:湖州 呼和浩特 中山 畢節 駐馬店 江蘇 股票 衡水

巨人網絡通訊聲明:本文標題《python tkinter 做個簡單的計算器的方法》,本文關鍵詞  python,tkinter,做個,簡單,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python tkinter 做個簡單的計算器的方法》相關的同類信息!
  • 本頁收集關于python tkinter 做個簡單的計算器的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 爽好舒服要喷了h文| 黑人玩弄,人妻,一区二区三区| 美容院特殊服8| 赤坂丽4部无删减版| 日本a级理论片免费看| 国产在线丝袜精品一区免费| 小说肉np| 很黄肉很黄的都市小说| 国产一级免费在线观看| 99精品无码一区二区学生| 6080私人午夜性爽快影院| 韩国三级中文字幕hd爽爽| 97精品久久天干天天蜜| ?国产精品嫩草久久久播放| 日产精密秘?入口麻豆29| 亚洲熟女的熟妇毛茸茸| 欧洲美女大白屁股αss| 校花被两根粗黑来回进出视频| 欧美亚洲国产另类无码综合网| japanese公交车上xxx| 久久av高潮av无码| 久久久AV波多野结衣一区二区| 跪成一排撅伺候奴主人| 91国在线高清视频| 3D欧美精品动漫XXXX无尽| 林阳苏颜免费小说| 亚洲日韩欧美综合中文字幕笕纯| 5060午夜一级毛片| 毛片A片| 亚洲地址一地址二地址三| 一人上面一个下日本人| SweetFox欧美在线观看| 爽死你个放荡粗暴小淫货男女视频| 日韩人妻无码精品一区二区三区| 床吻摸腿中间吃胸故事| 精品免费大国偷自产在线Av片| 色情a v| 校园春色激情网| 调教制服丝袜女警花| 巴西一级片| 蹂躏办公室波多野在线播放|