wxPython中如何把一个文本TextCtrl与&QUOT插入一个按钮;新增]按键 [英] wxPython how to put a text in TextCtrl with a button inserted by "Add" Button

查看:255
本文介绍了wxPython中如何把一个文本TextCtrl与&QUOT插入一个按钮;新增]按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用wxPython的建立一些GUI ......事实上,这并不是一件容易的程序...结果
要产生很多很多的用户输入和输出...

I'm using wxPython to build some GUI... Indeed it isn't an easy program...
Many many user inputs and outputs to be generated...

我把一个添加按钮,将动态添加TextCtrl字段和打开按钮,打开一个文件的程序的一个组成部分。点击打开,用户可以选择文件后,文件通路,因此显示该TextCtrl字段

One part of the program I put an "Add" button that will dynamically add a TextCtrl field and an "Open" button to open a file. After clicking in the "Open" the user can select a file, the file pathway is therefore showed in the TextCtrl field.

事实上,使用一个简单的例子(一个TextCtrl一个按钮)我能应付......结果
但是,在一个动态的方式,将几个TextCtrl和几个按钮,我不知道该如何处理呢?

Indeed, using a simple example (one TextCtrl one button) I can handle it...
But in a dynamically way, putting several TextCtrl and several Buttons I don't know how to handle it...

在以下code(仅适用于所有的一小部分,有些东西不应该在那里),我把高清OpenReadFile你可以打开按钮,点击后看到有,它会把文本在过去的TextCtrl没有在相应的领域...任何想法?

In the following code (only a small part of all, some stuff should not be there), I put a "def OpenReadFile" as you can see there after "Open" button click, it will put the text in the last TextCtrl not in the corresponding field... Any ideas?

在另一个的话...想象一下:
ADD(用户可以添加N个样本)

In another words... Imagine this: Add (the user can Add "n" samples)

"TEXT-1" 'BUTTON-1'
"TEXT-2" 'BUTTON-2'
"TEXT-3" 'BUTTON-3'

如果在按钮-3用户点击例如,它会放在文本3字段中的文本(如预期)结果
如果用户点击按钮-2,它会把文本文本3 ...

if the user click in button-3 for example it will put the text in Text-3 field (as expected)
if the user click in button-2, it will put the text in Text-3...

我的code到目前为止,(事实上,我知道那里的错误是,我只是不知道该怎么D0)= [

My code so far (Indeed I know where the mistake is, I just don't know what to d0) =[

self.addButton = wx.Button(self, label="Add Sample")
self.Bind(wx.EVT_BUTTON, self.OnAddWidget, self.addButton)
def OnAddWidget(self, event):
    self.samplenumber += 1
    self.sampleTextCtrl = wx.TextCtrl(self, wx.NewId(), "", size = (200,-1))
    self.buttonF = wx.Button(self, buttonId, label = "Select File") #Add Open File Button
    self.Bind(wx.EVT_BUTTON, self.OpenReadFile, self.buttonF) #Add click event
    self.fgs4.Add(self.buttonF, proportion = 1, flag = wx.CENTER, border = -1)
def OpenReadFile(self,event):
    dlg = wx.FileDialog(self, "Open File",
                os.getcwd(), style = wx.OPEN)

    if dlg.ShowModal() == wx.ID_OK:
        self.filename = dlg.GetPath()
        self.sampleTextCtrl.SetValue(self.filename)

我是pretty确保我必须做一些东西在OpenReadFile =]

I'm pretty sure that I do have to do some stuff in OpenReadFile =]

EDITED

我之前已经把一个字典,得到了按钮的ID和TextCtrl把它放在字典和完成。

I've put a dict before, got the button ID and the TextCtrl put it in the dict and done.

my_dict = {}
self.sampleTextCtrl = wx.TextCtrl(self, wx.NewId(), "", size = (200,-1))
value = self.sampleTextCtrl
buttonId = wx.NewId()
self.buttonF = wx.Button(self, buttonId, label = "Select File")

mydict[buttonId] = value

完成

推荐答案

使用字典肯定是一条路可走。另一种方法是使用Python的lambda声明通过配对文本控件来打开文件对话框的功能。通过做这种方式,您可以使用文本控件实例设置从对话框中值。下面是一些code演示概念:

Using a dictionary is certainly one way to go. Another way would be to use Python's lambda statement to pass the paired text control to the function that opens the file dialog. By doing it this way, you can use the text control instance to set the value from the dialog. Here's some code that demonstrates the concept:

import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.mainsizer = wx.BoxSizer(wx.VERTICAL)

        add_btn = wx.Button(self, label="Add")
        add_btn.Bind(wx.EVT_BUTTON, self.onAdd)
        self.mainsizer.Add(add_btn, 0, wx.ALL, 5)

    #----------------------------------------------------------------------
    def onAdd(self, event):
        """"""
        gen_sizer = wx.BoxSizer(wx.HORIZONTAL)

        txt = wx.TextCtrl(self, size=(600, -1))
        gen_sizer.Add(txt, 0, wx.ALL, 5)

        browse_btn = wx.Button(self, label='Browse')
        browse_evt = lambda evt, ctrl=txt: self.onBrowse(evt, ctrl)
        browse_btn.Bind(wx.EVT_BUTTON, browse_evt)
        gen_sizer.Add(browse_btn, 0, wx.ALL, 5)
        self.mainsizer.Prepend(gen_sizer, 0, wx.ALL, 5)
        self.mainsizer.Layout()


    #----------------------------------------------------------------------
    def onBrowse(self, event, ctrl):
        """"""
        wildcard = "Python source (*.py)|*.py|" \
            "All files (*.*)|*.*"
        dlg = wx.FileDialog(self, message="Choose a file",
                            defaultFile="", wildcard=wildcard,
                            style=wx.OPEN | wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            ctrl.SetValue(path)
        dlg.Destroy()


########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title='Dynamic file browser',
                          size=(1024,768))
        panel = MyPanel(self)
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

您可能还需要检查出更多的信息,以下链接:

You might also want to check out the following links for more information:

  • http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks
  • http://www.blog.pythonlibrary.org/2010/07/19/the-python-lambda/

这篇关于wxPython中如何把一个文本TextCtrl与&QUOT插入一个按钮;新增]按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆