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

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

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

一、前言

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

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

如果使用的是Linux系統(tǒng),可能需要安裝它:

$ pip install python-tk

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

圖片圖片

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

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

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

用法示例:

>>> 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行代碼,已經(jīng)在Python中制作了一個命令行計算器,現(xiàn)在讓我們使用相同的概念來制作一個帶有圖形界面的計算器。

這個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()

輸出:

圖片圖片

如上所示,我們已經(jīng)完成了顯示屏幕的構(gòu)建,現(xiàn)在需要添加一個按鈕用于形成數(shù)學表達式。

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

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

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

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

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()

輸出:

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

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

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

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()

輸出:

七、結(jié)語

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

搭建一個GUI計算器不僅僅是一個有趣的項目,它還展示了Python的強大和靈活性。希望對你有所幫助,并激勵你進一步探索和開發(fā)更多有趣的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

網(wǎng)絡架構(gòu)OSI

2020-11-06 08:54:43

Vue 3.0函數(shù)代碼

2009-11-17 12:47:05

PHP配置

2023-02-16 08:26:41

2024-03-21 09:51:22

Python爬蟲瀏覽網(wǎng)站

2022-11-03 16:41:08

2021-01-11 09:33:37

Maven數(shù)目項目

2025-04-07 05:00:00

2023-07-31 11:37:05

經(jīng)營分析模型

2022-07-27 15:50:55

漏洞網(wǎng)絡攻擊

2014-08-11 17:30:52

BlackphoneRootDef Con

2024-07-10 18:55:09

Python定時

2022-03-04 16:06:33

數(shù)據(jù)庫HarmonyOS鴻蒙

2020-07-17 07:44:25

云計算邊緣計算IT

2025-03-12 10:05:01

運維Vim編輯

2023-07-02 16:34:06

GPU虛擬化深度學習

2025-06-13 07:58:58

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲人在线观看视频 | 日韩欧美中文 | 亚洲精品电影在线观看 | 色一阁| 99精品欧美一区二区三区综合在线 | 羞羞网站在线免费观看 | 韩日精品一区 | 午夜影院在线观看视频 | 黑人巨大精品欧美黑白配亚洲 | 久久国产精品免费一区二区三区 | 91av在线看 | 久久国产精99精产国高潮 | 欧美日韩国产中文字幕 | 亚洲精品视频一区二区三区 | 亚洲福利| 欧美日韩国产三级 | 国产一区不卡 | 亚洲手机视频在线 | 欧美激情精品久久久久 | 日韩av在线播 | 91在线视频观看免费 | 精品一区二区视频 | 中文字幕亚洲欧美日韩在线不卡 | 成在线人视频免费视频 | 午夜影视免费片在线观看 | 亚洲国内精品 | 99精品一级欧美片免费播放 | 久久黄视频 | 亚洲狠狠| 日韩欧美在线播放 | 国产美女在线免费观看 | 亚洲国产精品久久久久 | 啪啪av| 欧美久久电影 | 国产大学生情侣呻吟视频 | 亚洲黄色一级 | 99久久精品国产毛片 | 搞黄网站在线观看 | 亚洲一区二区中文字幕 | 欧洲尺码日本国产精品 | 欧美性jizz18性欧美 |