【问题标题】:How to take value of ttk.Entry and display it in a messagebox?如何获取 ttk.Entry 的值并将其显示在消息框中?
【发布时间】:2016-12-20 13:36:46
【问题描述】:

我刚开始使用 tkinter 和 python3。我正在使用 pygubu 尝试制作一个非常简单的方法,它从 Entry 小部件中获取输入,并在按下按钮时将其显示在 Messagebox 中。以下是我正在使用的代码:

# command_properties.py
import tkinter as tk
from tkinter import messagebox
import pygubu

# define the function callbacks
def on_button1_click():
    tk_messageBox -message answer -type ok -icon info

class MyApplication(pygubu.TkApplication):

    def _create_ui(self):
        #1: Create a builder
        self.builder = builder = pygubu.Builder()

        #2: Load an ui file
        builder.add_from_file('test.ui')

        #3: Create the widget using self.master as parent
        self.mainwindow = builder.get_object('Frame_1', self.master)

        # Configure callbacks
        callbacks = {
            'on_button1_clicked': on_button1_click
        }

        builder.connect_callbacks(callbacks)


if __name__ == '__main__':
    root = tk.Tk()
    app = MyApplication(root)
    app.run()

这里是ui文件代码:http://pastebin.com/puRbD87m 另外,请告诉我我具体哪里错了。

【问题讨论】:

  • 您似乎没有尝试过完成这项工作。您需要尝试创建on_button1_click 函数,然后在遇到更具体的问题时提出问题,然后“告诉我我错在哪里”。此外,本网站强烈不鼓励链接到其他网站上的代码。这可能会发生变化,或者该网站可能会关闭,从而使这个问题在未来对其他人毫无用处。

标签: python-3.x tkinter ttk


【解决方案1】:

在类中创建on_button1_click()作为方法然后你可以创建

self.entry_1 = self.builder.get_object('Entry_1', self.master)

并使用

访问文本
self.entry_1.get()

完整示例

import tkinter as tk
import pygubu

class MyApplication(pygubu.TkApplication):

    def _create_ui(self):
        self.builder = pygubu.Builder()

        self.builder.add_from_file('test.ui')

        mainwindow = builder.get_object('mainwindow', master)

        self.entry_1 = self.builder.get_object('Entry_1', self.master) # <- self.entry

        callbacks = {
            'on_button1_clicked': self.on_button1_click # <- self.
        }

        self.builder.connect_callbacks(callbacks)

    def on_button1_click(self): # <- self
        print(self.entry_1.get())

if __name__ == '__main__':
    root = tk.Tk()
    app = MyApplication(root)
    app.run()

【讨论】:

  • 我试过了,但我只得到了一个 Entry 小部件,我的 .ui 文件中没有其他任何东西
  • 我不知道如何使用其他元素。没有文件,它已经有 4 年历史了。我不使用任何 RAD 工具 - 它提供完全控制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多