I want to know how I can use the value from a text entry dialog
from the def textentry function in the def __init__ function of the
class such as in the wx.ListBox where asd is printing on the right
side.
Here is the code :
import wx
class main_window(wx.Frame):
def SetOutput(self, output):
self.output = output
def OnSelChanged(self, event):
"""
If an output function is defined, we try to print some
informative, interesting and thought-provoking stuff to it.
If it has a __doc__ string, we print it. If it's a function or
unbound class method, we attempt to find the python source.
"""
item = event.GetItem()
def textentry(self, event):
dlg = wx.TextEntryDialog(self, 'Enter URL','URL Parsing')
dlg.SetValue("Default")
if dlg.ShowModal() == wx.ID_OK:
self.SetStatusText('You entered: %s\n' % dlg.GetValue())
return (dlg.GetValue())
def opendir(self, event):
dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
if dlg.ShowModal() == wx.ID_OK:
self.SetStatusText('You selected: %s\n' % dlg.GetPath())
dlg.Destroy()
def OnExit(self,e):
self.Close(True)
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(500, 500),style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
status=self.CreateStatusBar()
splitter = wx.SplitterWindow(self, style=wx.SP_3D)
splitter.SetMinimumPaneSize(1)
menubar=wx.MenuBar()
first=wx.Menu()
second=wx.Menu()
third=wx.Menu()
first.Append(106,"a","a")
first.Append(104,"Open","Browse")
first.Append(100,"anything","yup")
first.Append(105,"Exit","Quit")
second.Append(101,"s","s")
menubar.Append(first,"File")
menubar.Append(second,"Tool")
menubar.Append(third,"Contact us")
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.textentry, id=106)
self.Bind(wx.EVT_MENU, self.OnExit, id=105)
self.Bind(wx.EVT_MENU, self.opendir, id=104)
self.tree = wx.TreeCtrl(splitter,1, style=wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
"""
If an output function is defined, we try to print some
informative, interesting and thought-provoking stuff to it.
If it has a __doc__ string, we print it. If it's a function or
unbound class method, we attempt to find the python source.
"""
root = self.tree.AddRoot('wd')
os = self.tree.AppendItem(root, 'sa')
cl = self.tree.AppendItem(root, 'a')
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree)
cunt=wx.ListBox(splitter, -1, (100,100), (100,100), 'asd', wx.LB_SINGLE)
cunt.SetSelection(1)
splitter.SplitVertically(self.tree, cunt,200)
splitter.SetSashPosition(180, True)
self.Show(True)
"""
If an output function is defined, we try to print some
informative, interesting and thought-provoking stuff to it.
If it has a __doc__ string, we print it. If it's a function or
unbound class method, we attempt to find the python source.
"""
class App(wx.App):
def OnInit(self):
frame = main_window(None, 'S2A vulnerability Scanner')
return True
if __name__ == '__main__':
app = App(0)
app.MainLoop()
You just need to insert the input value into the list box.
use self.cunt to replace the cunt
in textentry use self.cunt.Insert(val, 0) to insert the input value into list
Try following code:
import wx
class main_window(wx.Frame):
def SetOutput(self, output):
self.output = output
def OnSelChanged(self, event):
"""
If an output function is defined, we try to print some
informative, interesting and thought-provoking stuff to it.
If it has a __doc__ string, we print it. If it's a function or
unbound class method, we attempt to find the python source.
"""
item = event.GetItem()
def textentry(self, event):
dlg = wx.TextEntryDialog(self, 'Enter URL','URL Parsing')
dlg.SetValue("Default")
if dlg.ShowModal() == wx.ID_OK:
val = dlg.GetValue()
self.SetStatusText('You entered: %s\n' % val)
self.cunt.Insert(val, 0)
return (dlg.GetValue())
def opendir(self, event):
dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
if dlg.ShowModal() == wx.ID_OK:
self.SetStatusText('You selected: %s\n' % dlg.GetPath())
dlg.Destroy()
def OnExit(self,e):
self.Close(True)
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(500, 500),style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
status=self.CreateStatusBar()
splitter = wx.SplitterWindow(self, style=wx.SP_3D)
splitter.SetMinimumPaneSize(1)
menubar=wx.MenuBar()
first=wx.Menu()
second=wx.Menu()
third=wx.Menu()
first.Append(106,"a","a")
first.Append(104,"Open","Browse")
first.Append(100,"anything","yup")
first.Append(105,"Exit","Quit")
second.Append(101,"s","s")
menubar.Append(first,"File")
menubar.Append(second,"Tool")
menubar.Append(third,"Contact us")
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.textentry, id=106)
self.Bind(wx.EVT_MENU, self.OnExit, id=105)
self.Bind(wx.EVT_MENU, self.opendir, id=104)
self.tree = wx.TreeCtrl(splitter,1, style=wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
"""
If an output function is defined, we try to print some
informative, interesting and thought-provoking stuff to it.
If it has a __doc__ string, we print it. If it's a function or
unbound class method, we attempt to find the python source.
"""
root = self.tree.AddRoot('wd')
os = self.tree.AppendItem(root, 'sa')
cl = self.tree.AppendItem(root, 'a')
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree)
self.cunt=wx.ListBox(splitter, -1, (100,100), (100,100), 'asd', wx.LB_SINGLE)
self.cunt.SetSelection(1)
splitter.SplitVertically(self.tree, self.cunt,200)
splitter.SetSashPosition(180, True)
self.Show(True)
"""
If an output function is defined, we try to print some
informative, interesting and thought-provoking stuff to it.
If it has a __doc__ string, we print it. If it's a function or
unbound class method, we attempt to find the python source.
"""
class App(wx.App):
def __init__(self, redirect):
wx.App.__init__(self, redirect)
def OnInit(self):
frame = main_window(None, 'S2A vulnerability Scanner')
return True
if __name__ == '__main__':
app = App(0)
app.MainLoop()
Related
I am trying to learn OOP and wx Python concurrently, and I am finding creating an instance of a class when using wx difficult. I don't know what I am missing, but see the code below and you can see my confusion with the last line before the loop. In a normal inheritance scenario, I could create an instance of the class DailyTasks and then call a method using that instance. Why not in wx? Is it because I am not using the object class in the parent class and instead have to use wx.Frame and wx.Panel? I hope this makes sense what I am asking. The first part of the code is the behavior that I would expect without wx, and the second group of code is the wx code that is confusing me. Thanks in advance for your help.
----------
class Animals(object):
def __init__(self, name):
self.name = name
def eat(self, food):
print '%s eats the %s'% (self.name, food)
def breath(self):
print '%s breaths through lungs'%(self.name)
class Dog(Animals):
def gets_mad(self):
print 'When %s gets mad, %s bites'%(self.name, self.name)
def show_affection(self):
print '%s shows affection and wags his tail'% (self.name)
def fetch(self,thing):
print '%s chases and brings back the %s'% (self.name, thing)
class Cat(Animals):
def gets_mad(self):
print 'When %s gets mad, %s will scratch you'%(self.name, self.name)
def show_affection(self):
print '%s shows affection and purrs'% (self.name)
def fetch(self):
print "%s doesn't fetch anything" % (self.name)
r = Dog('Rover') #instantiating to Dog class
c = Cat('Fluffy')
c.show_affection()
r.show_affection()
c.fetch()
r.fetch('ball')
r.eat('dogfood')
c.eat('catfood')
c.gets_mad()
r.gets_mad()
rex = Dog('Ralph')
rex.eat('steak')
----------
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='My File Organization')
book = wx.Notebook(self)
page = Settings(parent=book)
page2 = DailyTasks(parent=book)
book.AddPage(page, 'Settings')
book.AddPage(page2,'Daily Tasks')
self.SetSize(wx.Size(1024,768))
self.Centre()
class Settings(wx.Panel):
def __init__(self, parent):
self.parent = parent
wx.Panel.__init__(self, parent)
self.panel = wx.Panel(self, pos = (0,0),
size=(1024,768))
self.file_text = wx.StaticText(self.panel,
-1, "Daily Tasks Button 1 Path", (40,20))
self.file_text_box = wx.TextCtrl(self.panel,
-1,pos = (170,15), size = (240,20), style = 0, value = 'C:\Documents')
self.file_button_text = wx.StaticText(self.panel,
-1, "Button Name", (420,20))
self.file_button_text_box = wx.TextCtrl(self.panel,
-1,pos = (500,15), size = (140,20), style = 0, value = 'Zombies')
self.file_text2 = wx.StaticText(self.panel, -1,
"Daily Tasks Button 2 Path", (40,50))
self.file_text_box2 = wx.TextCtrl(self.panel,
-1,pos = (170,45), size = (240,20), style = 0, value = 'C:\\')
self.file_button_text2 = wx.StaticText(self.panel,
-1, "Button Name", (420,50))
self.file_button_text_box2 = wx.TextCtrl(self.panel,
-1,pos = (500,45), size = (140,20), style = 0, value = 'Angels')
class DailyTasks(Settings):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.parent = None
self.panel = wx.Panel(self, pos = (0,0), size=(1024,768))
def test(self):
print 'testing'
dt = DailyTasks() # This is where I have the question and need to create
# an instance of the DailyTasks class.
if __name__ == "__main__":
app = wx.App()
MyFrame().Show()
app.MainLoop()
wx should allow inheritance in the normal manner. Can you expand on what the actual behavior is when you assign dt to your instance of DailyTask()
How to call function when double click on label? Now it call on single click .What use instead of on_ref_press?
layout.add_widget(
MyLabel(text='[ref=world]' + str('Test') + '[/ref]', padding_x=10,
size_hint_x=.35, halign='left',
markup=True, on_ref_press=partial(self.xyz, 10)))
A possible solution is to create the event, in the following code I show an example:
from kivy.app import App
from kivy.uix.label import Label
class DoubleClickableLabel(Label):
def __init__(self, **kwargs):
Label.__init__(self, **kwargs)
self.register_event_type('on_double_press')
if kwargs.get("on_double_press") is not None:
self.bind(on_double_press=kwargs.get("on_double_press"))
def on_touch_down(self, touch):
if touch.is_double_tap:
self.dispatch('on_double_press', touch)
return True
return Label.on_touch_down(self, touch)
def on_double_press(self, *args):
pass
class MyApp(App):
def build(self):
label = DoubleClickableLabel(text='Hello world', on_double_press=self.callback)
return label
def callback(self, *args):
print("double clicked", args[0])
if __name__ == '__main__':
MyApp().run()
How to modify the combobox in wxPython to dropdown a checklistbox so I can select the choices?
something like above. I know that ComboBox class is available and I need to inherit it add the feature of checkbox to dropdown list. But I am not getting the proper way to start with it.
Take a look at the ComboCtrl class. It allows you to provide the window that implements the combo's drop-down. There are some examples in the wxPython demo.
I haven't had the time to grind through ComboCtrl and it looks daunting.
However, an approximation of what you want can be achieved by torturing a wx.Dialog.
import wx
import wx.lib.scrolledpanel as scrolled
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "CheckBox Dialog",size=(400,250))
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.log = wx.TextCtrl(self.panel, wx.ID_ANY, size=(350,150),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
self.button = wx.Button(self.panel, label="Choose Colours")
sizer.Add(self.log, 0, wx.EXPAND | wx.ALL, 10)
sizer.Add(self.button, 0, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnButton)
self.panel.options = ['Red','Green','Black','White','Orange','Blue','Yellow']
self.panel.selected = [0,0,0,0,0,0,0]
def OnButton(self,event):
dlg = ShowOptions(parent = self.panel)
dlg.ShowModal()
if dlg.result:
result_text = 'Selected: '
for item in range(len(dlg.result)):
if dlg.result[item]:
result_text += self.panel.options[item]+' '
self.log.AppendText(result_text+'\n\n')
self.panel.selected = dlg.result
else:
self.log.AppendText("No selection made\n\n")
dlg.Destroy()
class ShowOptions(wx.Dialog):
def __init__(self, parent):
self.options = parent.options
self.selected = parent.selected
o_str = ''
for item in self.options:
o_str = o_str+item+','
wx.Dialog.__init__(self, parent, wx.ID_ANY, "CheckBoxes", size= (400,250))
self.top_panel = wx.Panel(self,wx.ID_ANY)
self.avail_options = wx.TextCtrl(self.top_panel, wx.ID_ANY, o_str,style = wx.TE_READONLY)
self.bot_panel = wx.Panel(self,wx.ID_ANY)
self.scr_panel = scrolled.ScrolledPanel(self,wx.ID_ANY)
top_sizer = wx.BoxSizer(wx.VERTICAL)
scr_sizer = wx.BoxSizer(wx.VERTICAL)
bot_sizer = wx.BoxSizer(wx.VERTICAL)
self.items = []
for item in range(len(self.options)):
self.item = wx.CheckBox(self.scr_panel,-1,self.options[item])
self.item.SetValue(self.selected[item])
self.items.append(self.item)
self.item.Bind(wx.EVT_CHECKBOX, self.Select)
self.saveButton =wx.Button(self.bot_panel, label="Save")
self.closeButton =wx.Button(self.bot_panel, label="Cancel")
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveOpt)
self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
top_sizer.Add(self.avail_options,0,flag=wx.EXPAND)
for item in self.items:
scr_sizer.Add(item,0)
bot_sizer.Add(self.saveButton,0,flag=wx.CENTER)
bot_sizer.Add(self.closeButton,0,flag=wx.CENTER)
self.scr_panel.SetupScrolling()
self.top_panel.SetSizer(top_sizer)
self.scr_panel.SetSizer(scr_sizer)
self.bot_panel.SetSizer(bot_sizer)
mainsizer = wx.BoxSizer(wx.VERTICAL)
mainsizer.Add(self.top_panel,0,flag=wx.EXPAND)
mainsizer.Add(self.scr_panel,1,flag=wx.EXPAND)
mainsizer.Add(self.bot_panel,0,flag=wx.EXPAND)
self.SetSizer(mainsizer)
self.Select(None)
self.Show()
def Select(self, event):
selection = []
for item in self.items:
x = item.GetValue()
selection.append(x)
selected_text = ''
for item in range(len(selection)):
if selection[item]:
selected_text += self.options[item]+' '
self.avail_options.SetValue(selected_text)
def OnQuit(self, event):
self.result = None
self.Destroy()
def SaveOpt(self, event):
self.result = []
for item in self.items:
x = item.GetValue()
self.result.append(x)
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
I created a widget using wx.ComboCtrl, as suggested by Robin Dunn.
Feel free to use it, or modify it any way you like.
You can also find a more advanced version of it on my GitHub account.
import wx
import wx.lib.mixins.listctrl
import operator
import functools
import contextlib
#contextlib.contextmanager
def asCM(function, *args, **kwargs):
"""Used to build with wxWidgets as context managers to help organize code."""
yield function(*args, **kwargs)
class CheckListCtrl(wx.ComboCtrl):
"""A wxListCtrl-like widget where each item in the drop-down list has a check box.
Modified code from: https://github.com/wxWidgets/Phoenix/blob/master/demo/ComboCtrl.py
"""
def __init__(self, parent, myId = None, initial = None, position = None, size = None, readOnly = False, **kwargs):
"""
parent (wxWindow) – Parent window (must not be None)
initial (str) – Initial selection string
readOnly (bool) - Determiens if the user can modify values in this widget
Example Input: CheckListCtrl(self)
"""
self.parent = parent
#Configure settings
style = []
if (readOnly):
style.append(wx.CB_READONLY)
#Create object
super().__init__(parent,
id = myId or wx.ID_ANY,
value = initial or "",
pos = position or wx.DefaultPosition,
size = size or wx.DefaultSize,
style = functools.reduce(operator.ior, style or (0,)))
self.popup = self.MyPopup(self, **kwargs)
def Append(self, *args, **kwargs):
self.popup.Append(*args, **kwargs)
class MyPopup(wx.ComboPopup):
"""The popup control used by CheckListCtrl."""
def __init__(self, parent, *, popupId = None, multiple = True, prefHeight = None,
image_check = None, image_uncheck = None, lazyLoad = False):
"""
multiple (bool) - Determines if the user can check multiple boxes or not
lazyLoad (bool) - Determines if when Create() is called
- If True: Waits for the first time the popup is called
- If False: Calls it during the build process
prefHeight (int) - What height you would prefer the popup box use
- If None: Will calculate what hight to use based on it's contents
- If -1: Will use the default height
"""
self.parent = parent
self.prefHeight = prefHeight
self._buildVar_myId = popupId
self._buildVar_multiple = multiple
self._buildVar_lazyLoad = lazyLoad
self._buildVar_image_check = image_check
self._buildVar_image_uncheck = image_uncheck
super().__init__()
parent.SetPopupControl(self)
def Create(self, parent):
self.checkList = self.MyListCtrl(self, parent,
myId = self._buildVar_myId,
multiple = self._buildVar_multiple,
image_check = self._buildVar_image_check,
image_uncheck = self._buildVar_image_uncheck)
return True
def Append(self, *args, **kwargs):
self.checkList.Append(*args, **kwargs)
def GetControl(self):
return self.checkList
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
if (self.prefHeight is -1):
return super().GetAdjustedSize(minWidth, prefHeight, maxHeight)
elif (self.prefHeight is not None):
return (minWidth, min(self.prefHeight, maxHeight))
return self.checkList.GetBestSize(minWidth, prefHeight, maxHeight)
def LazyCreate(self):
return self._buildVar_lazyLoad
class MyListCtrl(wx.ListCtrl, wx.lib.mixins.listctrl.CheckListCtrlMixin):
"""Modified code from: https://github.com/wxWidgets/wxPython/blob/master/demo/CheckListCtrlMixin.py"""
def __init__(self, parent, root, *, myId = None, multiple = False, image_check = None, image_uncheck = None):
"""
multiple (bool) - Determines if the user can check multiple boxes or not
"""
self.parent = parent
#Configure settings
style = [wx.LC_LIST, wx.SIMPLE_BORDER]
if (not multiple):
style.append(wx.LC_SINGLE_SEL)
#Create object
wx.ListCtrl.__init__(self, root, id = myId or wx.ID_ANY, style = functools.reduce(operator.ior, style or (0,)))
wx.lib.mixins.listctrl.CheckListCtrlMixin.__init__(self, check_image = image_check, uncheck_image = image_uncheck)
def Append(self, value, default = False):
"""Appends the given item to the list.
value (str) - What the item will say
default (bool) - What state the check box will start out at
Example Input: Append("lorem")
Example Input: Append("lorem", default = True)
"""
n = self.GetItemCount()
self.InsertItem(n, value)
if (default):
self.CheckItem(n)
def GetBestSize(self, minWidth, prefHeight, maxHeight):
return (minWidth, min(prefHeight, maxHeight, sum(self.GetItemRect(i)[3] for i in range(self.GetItemCount())) + self.GetItemRect(0)[3]))
# this is called by the base class when an item is checked/unchecked
def OnCheckItem(self, index, state):
print(index, state)
if (__name__ == "__main__"):
class TestFrame(wx.Frame):
def __init__(self):
super().__init__(None, wx.ID_ANY, "Lorem Ipsum")
with asCM(wx.Panel, self, wx.ID_ANY) as myPanel:
with asCM(wx.BoxSizer, wx.VERTICAL) as mySizer:
with asCM(CheckListCtrl, myPanel, prefHeight = None) as myWidget:
myWidget.Append("lorem")
myWidget.Append("ipsum", default = True)
myWidget.Append("dolor")
mySizer.Add(myWidget, 0, wx.ALL, 5)
myPanel.SetSizer(mySizer)
####################################
app = wx.App(False)
frame = TestFrame()
frame.Show()
app.MainLoop()
I want to set value into sheet using thread.
import time
import wx
from wx.lib import sheet
from threading import Thread
EVT_RESULT_ID = wx.NewId()
def EVT_RESULT(win, func):
win.Connect(-1, -1, EVT_RESULT_ID, func)
class ResultEvent(wx.PyEvent):
def __init__(self, data):
wx.PyEvent.__init__(self)
self.SetEventType(EVT_RESULT_ID)
self.data = data
class TestThread(Thread):
def __init__(self, wxObject,sheet):
Thread.__init__(self)
self.wxObject = wxObject
self.sheet=sheet
self.start()
def run(self):
self.sheet.sheetload()
wx.PostEvent(self.wxObject, ResultEvent(self.sheet))
class MySheet(sheet.CSheet):
def __init__(self, parent):
sheet.CSheet.__init__(self, parent)
self.SetNumberRows(100)
self.SetNumberCols(30)
def sheetload(self):
self.SetNumberRows(200)
self.SetNumberCols(30)
self.EnableEditing(False)
for i in range(200):
for j in range(30):
self.SetCellValue(i,j,str(i))
class Newt(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None,-1, "Tutorial")
self.box = wx.BoxSizer(wx.VERTICAL)
toolbar2 = wx.ToolBar(self, wx.TB_HORIZONTAL | wx.TB_TEXT,size = (1000, 40))
self.show_btn =wx.Button(toolbar2, label="show")
self.show_btn.Enable(True)
self.Bind(wx.EVT_BUTTON, self.onShow,self.show_btn)
toolbar2.AddControl(self.show_btn)
self.box.Add((5,5) , 0)
self.box.Add(toolbar2)
self.box.Add((5,10) , 0)
toolbar2.Realize()
self.notebook = wx.Notebook(self, -1, style=wx.RIGHT)
self.box.Add(self.notebook, 1, wx.EXPAND)
self.Maximize(True)
self.SetSizer(self.box)
EVT_RESULT(self, self.updateDisplay)
def onShow(self, event):
sheet=MySheet(self.notebook)
TestThread(self,sheet)
def updateDisplay(self, msg):
t = msg.data
self.notebook.AddPage(t,"Logs")
app = wx.PySimpleApp()
frame = Newt().Show()
app.MainLoop()
Here I am using sheetload function defined in Mysheet to execute in TestThread. So i can set value into sheet in background without blocking main gui.
But i am getting this error, and my gui is crashing.
(python2.7:10775): GLib-CRITICAL **: Source ID 559 was not found when attempting to remove it
can you help me, what is wrong with this code.
I have created a setting window in python where i have a few path settings which has to be one time setting.Here is the sample demo code,
import wx
import os
class SettingWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
panel= wx.Panel(self,-1)
font = wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD)
field1 = wx.TextCtrl(panel,pos=(120,25),size=(170,20))
vsizer = wx.BoxSizer(wx.VERTICAL)
field1_sz=wx.BoxSizer(wx.HORIZONTAL)
field2_sz=wx.BoxSizer(wx.HORIZONTAL)
field1_lbl=wx.StaticText(panel,-1, label='Repo URL path:', pos=(25, 25))
field1_lbl.SetFont(font)
field1_sz.AddSpacer(50)
field1_sz.Add(field1_lbl)
field1_sz.AddSpacer(5) # put 5px of space between
field1_sz.Add(field1)
field1_sz.AddSpacer(50)
vsizer.AddSpacer(50)
vsizer.Add(field1_sz)
vsizer.AddSpacer(15)
vsizer.Add(field2_sz)
vsizer.AddSpacer(50)
btn1 = wx.Button(panel, label='Browse',pos=(300,25),size=(60,20))
btn1.Bind(wx.EVT_BUTTON, self.opendir)
def opendir(self, event):
dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
if dlg.ShowModal() == wx.ID_OK:
field1.SetValue("%s",dlg.GetPath())
dlg.Destroy()
class MyApp(wx.App):
def OnInit(self):
frame= SettingWindow(None,-1,'Setting Window')
frame.Show()
self.SetTopWindow(frame)
return True
app= MyApp(0)
app.MainLoop()
I want to display the path which i get from opendir in textCtrl.I am finding an error like below,
Traceback (most recent call last):
File "D:\PROJECT\SettingWindow.py", line 58, in opendir
return (field1)
NameError: global name 'field1' is not defined
use self.field1 instead of variable field1:
self.field1 = wx.TextCtrl(panel,pos=(120,25),size=(170,20))
self.field1.SetValue(dlg.GetPath())
This code works to make the TextCtrl field hidden when clicked on the checkbox
def OnCheckBox(self,event):
if self.checkbox.Value==False:
self.field.Enable(True)
else:
self.field.Enable(False)