成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

快速上手,五分鐘內完成個性化Python GUI計算器搭建

網絡 路由交換
在短短的五分鐘內,我們成功地使用Tkinter庫搭建了一個Python GUI計算器。這個計算器可以進行基本的數學運算,并為用戶提供了友好的交互體驗。

一、前言

在本教程中,你將學習如何在Python中使用Tkinter在短短幾分鐘內制作自己的全功能GUI計算器。

在完成本教程時,除了通常隨Python標準庫一起安裝的Tkinter之外,不需要任何額外的庫。

如果使用的是Linux系統,可能需要安裝它:

$ pip install python-tk

一切安裝完畢后,開始編寫我們的計算器代碼,在教程結束時,將搭建出類似下面的東西:

圖片圖片

二、使用eval()解決數學問題

eval()是Python中的一個內置函數,它會解析表達式參數并將其作為Python表達式進行求值。

我們將使用eval()的概念來解決數學表達式。

用法示例:

>>> while True:
...     expression = input('Enter equation: ')
...     result = eval(expression)
...     print(result)
... 
Enter equation: 2 + (9/9) *3
5.0
Enter equation: 12 /9 + (18 -2) % 5
2.333333333333333

使用這4行代碼,已經在Python中制作了一個命令行計算器,現在讓我們使用相同的概念來制作一個帶有圖形界面的計算器。

這個GUI計算器有三個主要部分:

  • 用于顯示表達式的屏幕(框架)
  • 保存表達式值的按鈕
  • 搭建計算器邏輯

三、為計算器制作一個框架

from tkinter import Tk, Entry, Button, StringVar
class Calculator:
    def __init__(self, master):
        master.title('Simple Calculator')
        master.geometry('360x260+0+0')
        master.config(bg='#438')
        master.resizable(False, False)
root = Tk()
calculator = Calculator(root)
root.mainloop()

輸出:

圖片圖片

四、添加一個屏幕來顯示表達式

from tkinter import Tk, Entry, Button, StringVar
class Calculator:
    def __init__(self, master):
        master.title('Simple Calculator')
        master.geometry('360x260+0+0')
        master.config(bg='#438')
        master.resizable(False, False)
               
        self.equation = StringVar()
        self.entry_value = ''
        Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0)
root = Tk()
calculator = Calculator(root)
root.mainloop()

輸出:

圖片圖片

如上所示,我們已經完成了顯示屏幕的構建,現在需要添加一個按鈕用于形成數學表達式。

五、添加用于形成數學表達式的按鈕

這些按鈕的創建方式相同,只是它們所存儲的值和它們的位置不同。用于形成數學表達式的按鈕包括:

  • 0到9的數字
  • 數學運算符+、-、/、%
  • 小數點
  • 括號()

我們需要為每個按鈕附加一個命令,以便當我們點擊它時,它就會顯示在顯示屏上。為此,編寫一個簡單的show()函數來實現這個功能。

from tkinter import Tk, Entry, Button, StringVar
class Calculator:
    def __init__(self, master):
        master.title('Simple Calculator')
        master.geometry('360x260+0+0')
        master.config(bg='#438')
        master.resizable(False, False)
               
        self.equation = StringVar()
        self.entry_value = ''
        Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0)
        
        Button(width=8, text = '(', relief ='flat', command=lambda:self.show('(')).place(x=0,y=50)
        Button(width=8, text = ')', relief ='flat', command=lambda:self.show(')')).place(x=90, y=50)
        Button(width=8, text = '%', relief ='flat', command=lambda:self.show('%')).place(x=180, y=50)
        Button(width=8, text = '1', relief ='flat', command=lambda:self.show(1)).place(x=0,y=90)
        Button(width=8, text = '2', relief ='flat', command=lambda:self.show(2)).place(x=90,y=90)
        Button(width=8, text = '3', relief ='flat', command=lambda:self.show(3)).place(x=180,y=90)
        Button(width=8, text = '4', relief ='flat', command=lambda:self.show(4)).place(x=0,y=130)
        Button(width=8, text = '5', relief ='flat', command=lambda:self.show(5)).place(x=90,y=130)
        Button(width=8, text = '6', relief ='flat', command=lambda:self.show(6)).place(x=180,y=130)
        Button(width=8, text = '7', relief ='flat', command=lambda:self.show(7)).place(x=0,y=170)
        Button(width=8, text = '8', relief ='flat', command=lambda:self.show(8)).place(x=180,y=170)
        Button(width=8, text = '9', relief ='flat', command=lambda:self.show(9)).place(x=90,y=170)
        Button(width=8, text = '0', relief ='flat', command=lambda:self.show(0)).place(x=0,y=210)
        Button(width=8, text = '.', relief ='flat', command=lambda:self.show('.')).place(x=90,y=210)
        Button(width=8, text = '+', relief ='flat', command=lambda:self.show('+')).place(x=270,y=90)
        Button(width=8, text = '-', relief ='flat', command=lambda:self.show('-')).place(x=270,y=130)
        Button(width=8, text = '/', relief ='flat', command=lambda:self.show('/')).place(x=270,y=170)
        Button(width=8, text = 'x', relief ='flat', command=lambda:self.show('*')).place(x=270,y=210)
def show(self, value):
        self.entry_value +=str(value)
        self.equation.set(self.entry_value)
    
root = Tk()
calculator = Calculator(root)
root.mainloop()

輸出:

輸出是一個帶有按鈕的計算器,當你點擊其中任意一個按鈕時,它的值就會顯示在顯示屏上。

現在我們的計算器只剩下兩個按鈕就能完整,一個是重置按鈕用于清除屏幕,另一個是等號(=)按鈕,用于計算表達式并將結果顯示在屏幕上。

六、為計算器添加重置和等號按鈕

from tkinter import Tk, Entry, Button, StringVar
class Calculator:
    def __init__(self, master):
        master.title('Simple Calculator')
        master.geometry('360x260+0+0')
        master.config(bg='#438')
        master.resizable(False, False)
               
        self.equation = StringVar()
        self.entry_value = ''
        Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0)
Button(width=8, text = '(', relief ='flat', command=lambda:self.show('(')).place(x=0,y=50)
        Button(width=8, text = ')', relief ='flat', command=lambda:self.show(')')).place(x=90, y=50)
        Button(width=8, text = '%', relief ='flat', command=lambda:self.show('%')).place(x=180, y=50)
        Button(width=8, text = '1', relief ='flat', command=lambda:self.show(1)).place(x=0,y=90)
        Button(width=8, text = '2', relief ='flat', command=lambda:self.show(2)).place(x=90,y=90)
        Button(width=8, text = '3', relief ='flat', command=lambda:self.show(3)).place(x=180,y=90)
        Button(width=8, text = '4', relief ='flat', command=lambda:self.show(4)).place(x=0,y=130)
        Button(width=8, text = '5', relief ='flat', command=lambda:self.show(5)).place(x=90,y=130)
        Button(width=8, text = '6', relief ='flat', command=lambda:self.show(6)).place(x=180,y=130)
        Button(width=8, text = '7', relief ='flat', command=lambda:self.show(7)).place(x=0,y=170)
        Button(width=8, text = '8', relief ='flat', command=lambda:self.show(8)).place(x=180,y=170)
        Button(width=8, text = '9', relief ='flat', command=lambda:self.show(9)).place(x=90,y=170)
        Button(width=8, text = '0', relief ='flat', command=lambda:self.show(0)).place(x=0,y=210)
        Button(width=8, text = '.', relief ='flat', command=lambda:self.show('.')).place(x=90,y=210)
        Button(width=8, text = '+', relief ='flat', command=lambda:self.show('+')).place(x=270,y=90)
        Button(width=8, text = '-', relief ='flat', command=lambda:self.show('-')).place(x=270,y=130)
        Button(width=8, text = '/', relief ='flat', command=lambda:self.show('/')).place(x=270,y=170)
        Button(width=8, text = 'x', relief ='flat', command=lambda:self.show('*')).place(x=270,y=210)
        Button(width=8, text = '=', bg='green', relief ='flat', command=self.solve).place(x=180, y=210)
        Button(width=8, text = 'AC', relief ='flat', command=self.clear).place(x=270,y=50)
def show(self, value):
        self.entry_value +=str(value)
        self.equation.set(self.entry_value)
      
    def clear(self):
        self.entry_value = ''
        self.equation.set(self.entry_value)
    
    def solve(self):
        result = eval(self.entry_value)
        self.equation.set(result)
    
root = Tk()
calculator = Calculator(root)
root.mainloop()

輸出:

七、結語

在短短的五分鐘內,我們成功地使用Tkinter庫搭建了一個Python GUI計算器。這個計算器可以進行基本的數學運算,并為用戶提供了友好的交互體驗。

搭建一個GUI計算器不僅僅是一個有趣的項目,它還展示了Python的強大和靈活性。希望對你有所幫助,并激勵你進一步探索和開發更多有趣的GUI應用程序!

責任編輯:武曉燕 來源: Python學研大本營
相關推薦

2025-05-22 10:00:00

DockerRedis容器

2022-02-23 20:38:32

云原生集群Postgres

2024-09-18 08:21:24

JavaScriptTypeScriptprototype

2022-12-16 09:55:50

網絡架構OSI

2020-11-06 08:54:43

Vue 3.0函數代碼

2009-11-17 12:47:05

PHP配置

2023-02-16 08:26:41

2022-11-03 16:41:08

2024-03-21 09:51:22

Python爬蟲瀏覽網站

2021-01-11 09:33:37

Maven數目項目

2025-04-07 05:00:00

2023-07-31 11:37:05

經營分析模型

2014-08-11 17:30:52

BlackphoneRootDef Con

2022-07-27 15:50:55

漏洞網絡攻擊

2024-07-10 18:55:09

Python定時

2022-03-04 16:06:33

數據庫HarmonyOS鴻蒙

2025-03-12 10:05:01

運維Vim編輯

2020-07-17 07:44:25

云計算邊緣計算IT

2023-07-02 16:34:06

GPU虛擬化深度學習

2025-06-13 07:58:58

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 午夜资源 | 成人h动漫精品一区二区器材 | 国产视频第一页 | 一区 | 精品国产一区三区 | 射久久 | 成人在线精品视频 | 国产影音先锋 | 国产一区二区在线播放 | 欧美性受xxx| 91精品国产综合久久久久久 | 久久国产精品视频 | 国产在线精品一区二区 | 日韩免费在线视频 | 欧美日韩一区二区三区视频 | 97久久精品 | 国产亚洲精品精品国产亚洲综合 | 久久精品国产一区二区电影 | 久久久久久综合 | 日本淫视频 | 成在线人视频免费视频 | 国产精品99一区二区 | 激情欧美一区二区三区中文字幕 | 亚洲成人日韩 | 久久一区二区三区免费 | 人人人干 | 一区二区精品 | 日韩久久久久 | 99精品在线 | 久久久做 | 国产精品不卡一区 | 国产一级特黄真人毛片 | 国产精品69毛片高清亚洲 | www.黄网| 五月婷婷婷 | 亚洲综合色自拍一区 | 一级黄色大片 | 亚洲视频在线播放 | 96国产精品久久久久aⅴ四区 | 丁香综合 | 色综合99|