Tkinter entry returns default value instead of entered value

PHPz
Release: 2024-02-06 10:59:53
forward
332 people have browsed it

Tkinter 条目返回默认值,而不是输入的值

问题内容

我试图让按钮返回我在“条目”字段中输入的任何值,但它始终返回 xml 文件中设置的默认值。该程序在报告值时似乎忽略了我在框中输入的内容。

这是我的代码:

from tkinter import *
import tkinter as tk
from formation import appbuilder

root = tk.tk()
root.withdraw()

app = appbuilder(path="calculator.xml")

def calculate(event=none):
    # event parameter needs to be there because using the bind method passes an event object
    # access the expr_var we created earlier to determine the current expression entered
    expr = app.expr_var.get()

    # evaluate the expression
    try:
        result = expr
    except exception:
        # if the expression entered was malformed and could not be evaluated
        # we will display an error message instead
        result = "invalid expression"

    # display the result
    app.result.config(text=result)


app.connect_callbacks(globals())

app.mainloop()
Copy after login

这将调用包含以下代码的 xml 文件:



        
        
        
        
        
        
        
    
    
    
    
        
    
Copy after login

我使用 formationstudio 构建 tkinter gui。 我不确定我做错了什么,或者我是否应该采取完全不同的方法。

正确答案

这是因为tk()有两个实例:

  • 您明确创建的
  • 隐式创建的 appbuilder

删除显式创建的实例。

from formation import AppBuilder

app = AppBuilder(path="calculator.xml")

def calculate(event=None):
    # event parameter needs to be there because using the bind method passes an event object
    # access the expr_var we created earlier to determine the current expression entered
    expr = app.expr_var.get()

    # evaluate the expression
    try:
        result = expr
    except Exception:
        # if the expression entered was malformed and could not be evaluated
        # we will display an error message instead
        result = "Invalid expression"

    # display the result
    app.result.config(text=result)


app.connect_callbacks(globals())

app.mainloop()
Copy after login

The above is the detailed content of Tkinter entry returns default value instead of entered value. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!