wxpython textctrl 绑定变量,wxPython中如何把一个文本TextCtrl与&QUOT插入一个按钮;新增]按键...

I'm using wxPython to build some GUI... Indeed it isn't an easy program...

Many many user inputs and outputs to be generated...

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.

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...

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?

In another words... Imagine this:

Add (the user can Add "n" samples)

"TEXT-1" 'BUTTON-1'

"TEXT-2" 'BUTTON-2'

"TEXT-3" 'BUTTON-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...

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)

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

EDITED

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

Done

解决方案

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值