使用 wx.TextCtrl 并在 wxpython 中单击按钮后显示数据的简单示例 - wx 新手

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14927584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:53:04  来源:igfitidea点击:

Simple example of using wx.TextCtrl and display data after button click in wxpython - new to wx

pythonwxpython

提问by Doublespeed

I am learning python and trying out wxpython for UI development (dont have UI exp either). I have been able to create a frame with a panel, a button and a text input box. I would like to be able to enter text into the textbox and have the program do things to the text entered into the box after I click the button. Can I get some guidence as to how to do this? for example, lets say I want to display the text entered into the wx.TextCtrl control on the panel.. How would i do that?

我正在学习 python 并尝试使用 wxpython 进行 UI 开发(也没有 UI exp)。我已经能够创建一个带有面板、按钮和文本输入框的框架。我希望能够在文本框中输入文本,并在单击按钮后让程序对输入到框中的文本进行处理。我可以得到一些关于如何做到这一点的指导吗?例如,假设我想在面板上显示输入到 wx.TextCtrl 控件中的文本。我该怎么做?

import wx
class ExamplePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.quote = wx.StaticText(self, label="Your quote :", pos=(20, 30))

        # A button
        self.button =wx.Button(self, label="Save", pos=(200, 325))

    self.lblname = wx.StaticText(self, label="Your name :", pos=(20,60))
    self.editname = wx.TextCtrl(self, value="Enter here your name", pos=(150, 60), size=(140,-1))


app = wx.App(False)
frame = wx.Frame(None)
panel = ExamplePanel(frame)
frame.Show()
app.MainLoop()

采纳答案by Fenikso

To do any GUI interactions you have to bind events to the widgets. You basically tell the wxPython app which method (event handler) should be called when some event (button pressed) occurs.

要进行任何 GUI 交互,您必须将事件绑定到小部件。您基本上告诉 wxPython 应用程序在发生某些事件(按下按钮)时应该调用哪个方法(事件处理程序)。

I would also consider learning sizers and using them for your layouts. I have changed your example a bit.

我也会考虑学习 sizer 并将它们用于您的布局。我稍微改变了你的例子。

import wx
class ExampleFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)     
        self.quote = wx.StaticText(self.panel, label="Your quote:")
        self.result = wx.StaticText(self.panel, label="")
        self.result.SetForegroundColour(wx.RED)
        self.button = wx.Button(self.panel, label="Save")
        self.lblname = wx.StaticText(self.panel, label="Your name:")
        self.editname = wx.TextCtrl(self.panel, size=(140, -1))

        # Set sizer for the frame, so we can change frame size to match widgets
        self.windowSizer = wx.BoxSizer()
        self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)        

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(5, 5)
        self.sizer.Add(self.quote, (0, 0))
        self.sizer.Add(self.result, (0, 1))
        self.sizer.Add(self.lblname, (1, 0))
        self.sizer.Add(self.editname, (1, 1))
        self.sizer.Add(self.button, (2, 0), (1, 2), flag=wx.EXPAND)

        # Set simple sizer for a nice border
        self.border = wx.BoxSizer()
        self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)

        # Use the sizers
        self.panel.SetSizerAndFit(self.border)  
        self.SetSizerAndFit(self.windowSizer)  

        # Set event handlers
        self.button.Bind(wx.EVT_BUTTON, self.OnButton)

    def OnButton(self, e):
        self.result.SetLabel(self.editname.GetValue())

app = wx.App(False)
frame = ExampleFrame(None)
frame.Show()
app.MainLoop()