python - 我如何通过组合使用列表中的值 - pysimplegui
问题描述
import PySimpleGUI as sg
storedvals = []
sg.LOOK_AND_FEEL_TABLE['Theme'] = {'BACKGROUND': '#292929',
'TEXT': '#009bff',
'INPUT': '#ffffff',
'TEXT_INPUT': '#000000',
'SCROLL': '#ffffff',
'BUTTON': ('#5cfd46', '#151515'),
'PROGRESS': ('#01826B', '#D0D0D0'),
'BORDER': 1, 'SLIDER_DEPTH': 0, 'PROGRESS_DEPTH': 0,
}
sg.theme("Theme")
def mainmenu():
mainmenu = [
[sg.Text("Welcome to the Calculator!")],
[sg.Text("What would you like to do:")],
[sg.Text("1) Add\n2) Subtract\n3) Multiply\n4) Divide\n5) Exit")],
[sg.Text("Please enter the number of the option you would like!")],
[sg.InputText("", key="in1"),sg.Text("", key="answer")],
[sg.ReadButton("Submit")]
]
mainmenu = sg.Window('Window that stays open', mainmenu)
while True:
button, values = mainmenu.Read()
if button is None:
break
choice = int(values["in1"])
mainmenu.FindElement("answer").Update(choice)
if choice == 1:
mainmenu.Close()
add()
if choice == 2:
mainmenu.Close()
subtract()
if choice == 3:
mainmenu.Close()
multiply()
if choice ==4:
mainmenu.Close()
divide()
if choice == 5:
exit()
def add():
add = [
[sg.Text("What numbers would you like to add together?")],
[sg.Text("Stored values are in the drop down option!")],
[sg.Combo([storedvals], size=(5,1), key="num1"), sg.Text("+"), sg.Combo([storedvals], size=(5,1), key="num2")],
[sg.Text("Do you want to store this value")],
[sg.Radio("Yes", "1", key="x"), sg.Radio("No", "1",)],
[sg.ReadButton("Add")],
[sg.Text("Answer:"), sg.Text("", size=(10,1), key="answer")],
[sg.Button("Return to the menu"), sg.Exit()],
]
add = sg.Window("", add)
while True:
button, values = add.Read()
if button is None:
break
num1 = int(values["num1"])
num2 = int(values["num2"])
answer = num1 + num2
add.FindElement("answer").Update(answer)
x = values["x"]
if x == 1:
storedvals.append(answer)
sg.Popup("You answer has been stored in position", storedvals.index(answer))
print(storedvals)
event, values = add.Read()
if event == "Return to the menu":
add.Close()
mainmenu()
mainmenu()
这是我的代码。该子程序完成后,可以返回主菜单再次使用计算器。我想要它,以便他们可以存储以前的计算答案,然后选择在以后的计算中使用它(有效)。我选择使用组合,但是,我收到一个错误,在研究了错误后,我找不到问题的修复/解决方法。任何提示都是有帮助的。
The error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
我尝试了多种不同的方法来尝试操作它,但我刚刚开始学习pysimple
GUI,所以我被卡住了。
解决方案
解决方案是添加element.update(values=answer)
,其中答案应该是添加一个完整的列表,而不仅仅是一个条目。
推荐阅读
- mysql-5.5 - 如何在 Linux 命令上使用 mysql 的 --pager 选项
- react-native - react-native PanResponder:OnMoveShouldSetPanResponder 不工作
- javascript - React Native - 如何处理 TextInput onChangeText 事件在执行函数之前等待?
- function - 如何使用关联常量来定义数组的长度?
- sql - 如何在同一张表的不同列上使用不同的where子句连接SQL语句
- apache-spark - 如何使用 Spark 并行化将 gzip 压缩的 csv 转换为镶木地板?
- php - 在php中返回上一页后会话数据没有被破坏
- go - 在我的代码中安全完成 goroutine 的正确方法是什么?
- javascript - 网页推送通知 js/php
- qt - Qt 创建器中的白色窗口