使用 PySimpleGUI 的计算器 – Python
先决条件: PySimpleGUI、评估
PySimpleGUI 是一个Python包,它使各个级别的Python程序员都可以创建 GUI。您可以使用包含小部件(它们在 PySimpleGUI 中称为“元素”)的“布局”来指定您的 GUI 窗口。在本文中,我们将学习如何在Python使用 PySimpleGUI 制作计算器。
在开始之前,我们需要安装这个包:
pip install PySimpleGUI
方法:
- 导入 PySimpleGUI 模块
- 创建 GUI 布局和窗口
- 向主窗口添加任意数量的小部件
- 在小部件上应用事件触发器。
下面是 GUI 的样子:
让我们使用Python PySimpleGUI 模块创建一个基于 GUI 的简单计算器,它可以执行基本算术运算的加法、减法、乘法和除法。
让我们一步一步地理解实现:-
Step1:创建文本框、按钮
为了创建一个文本框,我们将使用Txt()方法。
Syntax: Txt(Enter Text, *attr)
这里将使用ReadFormButton()方法使用读取按钮。
Syntax: ReadFormButton(Enter Button Text, *attr)
第二步:创建一个无限循环,读取按钮值并执行操作。
Python3
# Result Value
Result = ''
# Make Infinite Loop
while True:
# Button Values
button, value = form.Read()
# Check Press Button Values
if button=='c':
Result = ''
form.FindElement('input').Update(Result)
elif button=='«':
Result = Result[:-1]
form.FindElement('input').Update(Result)
elif len(Result) == 16 :
pass
# Results
elif button=='=':
Answer = eval(Result)
Answer = str(round(float(Answer),3))
form.FindElement('input').Update(Answer)
Result = Answer
# close the window
elif button=='Quit' or button==None:
break
else:
Result += button
form.FindElement('input').Update(Result)
Python3
# Import Module
from PySimpleGUI import *
# GUI Layout
layout = [[Txt('' * 10)],
[Text('', size = (15, 1), font = ('Helvetica', 18),
text_color = 'black', key = 'input')],
[Txt('' * 10)],
[ReadFormButton('c'), ReadFormButton('«')],
[ReadFormButton('7'), ReadFormButton('8'), ReadFormButton('9'), ReadFormButton('/')],
[ReadFormButton('4'), ReadFormButton('5'), ReadFormButton('6'), ReadFormButton('*')],
[ReadFormButton('1'), ReadFormButton('2'), ReadFormButton('3'), ReadFormButton('-')],
[ReadFormButton('.'), ReadFormButton('0'), ReadFormButton('='), ReadFormButton('+')],
]
# Set PySimpleGUI
form = FlexForm('CALCULATOR', default_button_element_size = (5, 2),
auto_size_buttons = False, grab_anywhere = False)
form.Layout(layout)
# Result Value
Result = ''
# Make Infinite Loop
while True:
# Button Values
button, value = form.Read()
# Check Press Button Values
if button == 'c':
Result = ''
form.FindElement('input').Update(Result)
elif button=='«':
Result = Result[:-1]
form.FindElement('input').Update(Result)
elif len(Result) == 16 :
pass
# Results
elif button == '=':
Answer = eval(Result)
Answer = str(round(float(Answer),3))
form.FindElement('input').Update(Answer)
Result = Answer
# close the window
elif button == 'Quit' or button == None:
break
else:
Result += button
form.FindElement('input').Update(Result)
以下是完整的实现:
蟒蛇3
# Import Module
from PySimpleGUI import *
# GUI Layout
layout = [[Txt('' * 10)],
[Text('', size = (15, 1), font = ('Helvetica', 18),
text_color = 'black', key = 'input')],
[Txt('' * 10)],
[ReadFormButton('c'), ReadFormButton('«')],
[ReadFormButton('7'), ReadFormButton('8'), ReadFormButton('9'), ReadFormButton('/')],
[ReadFormButton('4'), ReadFormButton('5'), ReadFormButton('6'), ReadFormButton('*')],
[ReadFormButton('1'), ReadFormButton('2'), ReadFormButton('3'), ReadFormButton('-')],
[ReadFormButton('.'), ReadFormButton('0'), ReadFormButton('='), ReadFormButton('+')],
]
# Set PySimpleGUI
form = FlexForm('CALCULATOR', default_button_element_size = (5, 2),
auto_size_buttons = False, grab_anywhere = False)
form.Layout(layout)
# Result Value
Result = ''
# Make Infinite Loop
while True:
# Button Values
button, value = form.Read()
# Check Press Button Values
if button == 'c':
Result = ''
form.FindElement('input').Update(Result)
elif button=='«':
Result = Result[:-1]
form.FindElement('input').Update(Result)
elif len(Result) == 16 :
pass
# Results
elif button == '=':
Answer = eval(Result)
Answer = str(round(float(Answer),3))
form.FindElement('input').Update(Answer)
Result = Answer
# close the window
elif button == 'Quit' or button == None:
break
else:
Result += button
form.FindElement('input').Update(Result)
输出: