Tkinter 항목은 입력된 값 대신 기본값을 반환합니다.

PHPz
풀어 주다: 2024-02-06 10:59:53
앞으로
380명이 탐색했습니다.

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()
로그인 후 복사

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

<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>
<tkinter.Tk
    xmlns:attr="http://www.hoversetformationstudio.com/styles/" 
    xmlns:layout="http://www.hoversetformationstudio.com/layouts/" 
    name="tk_1" 
    attr:layout="place" 
    layout:width="300" 
    layout:height="233" 
    layout:x="30" 
    layout:y="30">
        <meth name="title">
        <arg value="title" />
        </meth>
        <meth name="geometry">
        <arg value="300x200+0+0" />
        </meth>
        <meta major="6" minor="2" name="version" />
    <tkinter.StringVar attr:name="expr_var" attr:value="" />
    <tkinter.Entry 
        name="entry_1" 
        attr:textvariable="expr_var"
        layout:width="200" 
        layout:height="25" 
        layout:x="50" 
        layout:y="35" 
        layout:bordermode="outside" />
    <tkinter.Label
        name="result" 
        layout:width="200" 
        layout:height="25" 
        layout:x="50" 
        layout:y="74" 
        layout:bordermode="outside" />
    <tkinter.Button name="calculate" attr:text="button_1" layout:width="80" layout:height="25" layout:x="110" layout:y="133" layout:bordermode="outside">
        <event sequence="&lt;Button-1&gt;" handler="calculate" add="False" />
    </tkinter.Button>
</tkinter.Tk>
로그인 후 복사

我使用 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()
로그인 후 복사

위 내용은 Tkinter 항목은 입력된 값 대신 기본값을 반환합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:stackoverflow.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!