python - Appjar 标签不会更新
问题描述
链接到 pastebin 中的代码 我是 Python 新手,我试图创建一个计算器应用程序。我正在为我的计算器 GUI 使用 Appjar 库。计算器工作正常,但 appjar 标签“bar”不会更新,尽管变量累加器发生了变化。我试图添加循环、appjar 的 .after() 函数,并弄乱了 appjar 的自动更新系统,但我似乎无法解决问题。
#define variables
error=''
accumulator = 0
mem = 0
op = ''
#import GUI
from appJar import gui
app=gui("Grid Demo", "500x600")
app.setSticky("news")
app.setExpand("both")
app.setFont(20)
# Do the math will take the accumulator and memory registers and perform the prevailing operation on them.
# It returns the result, as an integer
def doTheMath(a,b,o):
if o == "add":
return a + b
if o == "sub":
return b - a
if o == "mul":
return a * b
if o == "div":
return b/a
#press function defines the output of each button.
def press(button):
global accumulator
global mem
global op
print ("A button was pressed: " + button)
if button == "C":
accumulator = 0
op = ""
mem = 0
elif button == "=":
accumulator = doTheMath(accumulator,mem,str(op))
mem = 0
op = ""
elif button == "+":
op = "add"
mem = accumulator
accumulator = 0
elif button == "-":
op = "sub"
mem = accumulator
accumulator = 0
elif button == "x":
op = "mul"
mem = accumulator
accumulator = 0
elif button == "÷":
op = "div"
mem = accumulator
accumulator = 0
else:
accumulator = accumulator * 10 + int(button)
print ("Acc: " + str(accumulator) + ", op: " + op + ", mem: " + str(mem))
app.go()
#define widgets in GUI
app.addLabel("bar", error+str(accumulator), 0, 0, 3)
app.addButtons(["1"], press, 3, 0)
app.addButtons(["2"], press, 3, 1)
app.addButtons(["3"], press, 3, 2)
app.addButtons(["4"], press, 2, 0)
app.addButtons(["5"], press, 2, 1)
app.addButtons(["6"], press, 2, 2)
app.addButtons(["7"], press, 1, 0)
app.addButtons(["8"], press, 1, 1)
app.addButtons(["9"], press, 1, 2)
app.addButtons(["0"], press, 4, 1)
app.addButtons(["+"], press, 3, 3)
app.addButtons(["-"], press, 4, 3)
app.addButtons(["x"], press, 2, 3)
app.addButtons(["÷"], press, 1, 3)
app.addButtons(["="], press, 4, 2)
app.addButtons(["C"], press, 4, 0)
app.go()
解决方案
.setlable()
每当“bar”中的任何变量发生更改时,都需要引用 Appjar ,我创建了一个名为 changeLable() 的函数,
def changeLabel():
app.setLabel("bar", error+str(accumulator))
当在 if 语句集的末尾引用时,将标签“bar”更改为error+str(accumulator
.
归功于杰森哈珀。
推荐阅读
- java - 使用 .toArray() 将 ArrayList 转换为 Int [] 数组?或者...?
- angular - 根据位置找出给定数字的小数分隔符
- visual-studio-code - How can I remove this Git helper button bar in VSCode?
- python - Load data from Google Datastore to Google Cloud ML Engine
- javascript - 如何“要求”一个 xml 文件(由 webpack 加载)以读取 javascript 中的 xml 内容
- oracle - 通过脚本动态重命名索引和约束
- java - How to show an input is invalid in this java code
- javascript - 渲染多个输入反应
- ios - 请求的资源不支持使用 Alamofire 的 http 方法“T”(或“ST”)
- java - 角度不使用 [UTC] 转换 json 日期