我尝试用PySimpleGUI做一个计算器,作为一个学校项目,我已经用它做了一个基本的GUI,但我很难使按钮的功能。
import PySimpleGUI as sg
def pressed_button_0():
button0 = 0
def pressed_button_1():
button1 = 1
def pressed_button_2():
button2 = 2
def pressed_button_3():
button3 = 3
def pressed_button_4():
button4 = 4
def pressed_button_5():
button5 = 5
def pressed_button_6():
button6 = 6
def pressed_button_7():
button7 = 7
def pressed_button_8():
button8 = 8
def pressed_button_9():
button9 = 9
problem = ''
layout_1 = [
[sg.Text('Calculator')],
[sg.Text(str(problem))],
[sg.Button('1'), sg.Button('2'), sg.Button('3'), sg.Button('÷')],
[sg.Button('4'), sg.Button('5'), sg.Button('6'), sg.Button('×')],
[sg.Button('7'), sg.Button('8'), sg.Button('9'), sg.Button('+')],
[sg.Button('.'), sg.Button('0'), sg.Button('='), sg.Button('-')]
]
sg.theme('dark grey 13')
window = sg.Window('Calculator', layout_1)
problem = ''
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
if event == '0':
pressed_button_0()
window.close()
我尝试将文本元素设置为变量,我认为当我按下按钮时会更新,但似乎不起作用,不确定我做错了什么
1条答案
按热度按时间qxsslcnc1#
变量
buttonX
只是一个变量,与GUI无关,您必须调用elemet.update(value=something)
,其中window[element_key]
可以找到该元素。