wxPython如何从按钮打开的对话框中检索信息

时间:2017-10-13 16:30:52

标签: python user-interface events wxpython

我正在尝试使用此代码使用wxpython通过按钮单击事件打开的文本输入对话框中提取数据。

import wx
class apple(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'PyLabs', size=(840,600))
        panel = wx.Panel(self)
        box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi')


        status_bar = self.CreateStatusBar()
        menu_bar = wx.MenuBar()
        options_menu = wx.Menu()
        options_menu.Append(wx.NewId(), "Settings", "OpenSettings...")
        options_menu.Append(wx.NewId(), "Advanced", "Check Advanced...")
        menu_bar.Append(options_menu, "Options")
        self.SetMenuBar(menu_bar)

        New_Experiment_Button = wx.Button(panel, pos=(10,10), label='New Experiment', size=(120, 40))
        answer = self.Bind(wx.EVT_BUTTON, self.openFrame, New_Experiment_Button)
        print(answer)


    def openFrame(self, event):
        box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi')
        if box.ShowModal() == wx.ID_OK:
            answer = str(box.getValue)
            event.Skip()
        return answer


if __name__=='__main__':
    app=wx.PySimpleApp()
    frame = apple(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

我是wxpython编码的新手,我不明白当从Bind()函数内部调用时,我是如何抓取按钮事件所拉取的数据的。

打印(答案)的输出为“无”

如果有人能帮助我,我们将不胜感激!

2 个答案:

答案 0 :(得分:0)

为什么不在函数中打印,为什么需要将其返回到def __init__

 def openFrame(self, event):
    box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi')
    if box.ShowModal() == wx.ID_OK:
        answer = box.GetValue()
        event.Skip()
    print answer

答案 1 :(得分:0)

“回答”是import QtQuick 2.0 import QtQuick.Controls 1.4 ComboBox { id: combobox_ctrl Component.onCompleted: { for (var i = 0; i < combobox_ctrl.children.length; ++i) { if (combobox_ctrl.children[i].hasOwnProperty('onWheel') && combobox_ctrl.children[i] !== mouseArea) { combobox_ctrl.children[i].destroy() } } } MouseArea { id: mouseArea anchors.fill: parent onPressed: { if (combobox_ctrl.activeFocusOnPress) forceActiveFocus() combobox_ctrl.__popup.toggleShow() } onClicked: { combobox_ctrl.__popup.toggleShow() } } } ,但是您的实验还有一些问题,请参阅下文,检查差异

参考您的评论进行编辑,将结果存储为NonScrollingComboBox { anchors.verticalCenter: parent.verticalCenter model: ["item one", "item 2"] } 变量,并查看按钮上box.GetValue()语句的差异。

self