I've been using Tix to create a comboBox and it causes an intermittent crash if the entry box is left empty.
I'm new to Python and very new to GUI programming so I've been using example to teach myself stuff.
When using the following example code, you should be able to enter a value into the entry box or select form the dropdown menu, however if you leave the entry field empty and press go it will cause the python to crash.
import Tix
import tkMessageBox
class App(object):
def __init__(self, window):
window.winfo_toplevel().wm_title("test")
self.window = window
self.combo = Tix.ComboBox(window)
self.combo.insert(Tix.END, 'thing1')
self.combo.insert(Tix.END, 'thing2')
self.combo.entry['state'] = "normal"
self.combo['editable'] = True
self.combo.pack()
button = Tix.Button(window)
button['text'] = "Go"
button['command'] = self.go
button.pack()
def go(self):
tkMessageBox.showinfo('info', self.combo['selection'])
if __name__ == '__main__':
root = Tix.Tk()
App(root)
root.mainloop()
=========================
CRASH DETAILS :
Problem signature:
Problem Event Name: APPCRASH
Application Name: python2.7.exe
Application Version: 0.0.0.0
Application Timestamp: 4cfbf049
Fault Module Name: ntdll.dll
Fault Module Version: 6.1.7600.16385
Fault Module Timestamp: 4a5bdb3b
Exception Code: c0000005
Exception Offset: 0002e23e
OS Version: 6.1.7600.2.0.0.256.48
Locale ID: 2057
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
So
A] is it something the example is doing wrong ?
B] is there a better way?
C] Should I be using Tix at all ?
I've noticed that some people seem to shy away from Tix and only use Tkinter. is there a reason for this? Should I not be using Tix ?
++++++++++++++++++++++++++++
Ok so I managed to monkeypatch this by adding value =' ' so the return string is not empty and then lstrip() the value when I need it.
self.combo = Tix.ComboBox(window, value =' ')
...
tkMessageBox.showinfo('info', self.combo['selection'].lstrip())
Very ugly but stops me from crashing for now; until someone wiser can give me a better answer.
If the user deletes the space and then presses 'go' it will of cause crash again!
I am not Tix expert, but this should fix the problem:
tkMessageBox.showinfo('info', self.combo.entry.get())
In between the lines, I would recommend using wxPython for learning GUI programming. It was great for me as a learning environment.
import wx
class MainWin(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.combo = wx.ComboBox(self, choices=["Thing 1", "Thing 2"])
self.button = wx.Button(self, label="Go")
self.button.Bind(wx.EVT_BUTTON, self.OnButton)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.combo, 0, wx.EXPAND)
self.sizer.Add(self.button, 0, wx.EXPAND)
self.SetSizerAndFit(self.sizer)
self.Show()
def OnButton(self, e):
wx.MessageBox(self.combo.GetValue())
app = wx.App(False)
main_win = MainWin(None)
app.MainLoop()
Related
I'm kind of new with wxpython and python itself, sorry for the basic question.
I'm trying to organize my code in a more easy way to manage it. I created this simple example that kind of resume my problem. Basically it is just a window with a button that print a message. I separated it in three simple packages
ef_Main.py - This is the main packages it will import the UI and the application itself.
ef_Tool.py - it is the application that will run all the important code, right now it is just a print statment but will have all the application code.
ef_UI.py - a very basic interface using wxpython.
How it should work:
Run ef_Main.py it will import the interface (ef_UI.py) and the main code (ef_Tool.py). When something is click in the interface it will be ready by the ef_Main and send to ef_Tool to be executed.
My problem is:
I'm not sure how to use bind function to connect this three packages. I believe that it should be in the ef_Main but how it will get the info from the interface and send it to the ef_Tool.py.
And if I want to get some output from the ef_Tool and send it back to the interface. How should I do that.
Here is my code.
#ef_Main.py
import wx
import ef_UI as eU
import ef_Tool as eT
''' Here is where I don't know how to make it works,
if I should put a class here or not, and how to bind
this function with the impTool and impUI'''
#class MyMain(self):
def uiControls(self):
self.Bind(wx.EVT_BUTTON, eU.OnClick(self), eT.MyTools(self))
def main():
app = wx.App(False)
frame = eU.MyFrame()
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()
=======================
#ef_Tool.py
import wx
'''just a simple as possible function to be execute when it is called '''
class MyTools():
def OnClick(self, event):
#Value = self.MyTextCtrl.GetValue()
print "it is working! "
=======================
#ef_UI.py
import wx
''' very simple interface with only a button and a TextCtrl '''
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Menu Test")
self.panel = wx.Panel(self)
self.MyButton = wx.Button(self.panel, -1, "Button_test", (0, 0))
self.MyTextCtrl = wx.TextCtrl(self.panel, -1, value="just a test", pos=(100, 0))
Thanks in advance!
Emerson
Here is a simple example of fulfilling your requirements. Supposing all your application logic is done in ef_Tool.py and the input for these logic is from ef_UI and the output is also sent to ef_UI.py.
You need to call a method in ef_Tool.py when a button click event occurs in ef_UI. You can invoke this method from MyFrame's method. But you need an obj of MyTools to do this.
so first, create an obj for MyTools in ef_Main.py and pass this object to MyFrame
#ef_Main.py
import wx
import ef_UI as eU
import ef_Tool as eT
def main():
efToolObj = eT.MyTools() # object of MyTools class
app = wx.App(False)
frame = eU.MyFrame(efToolObj) # Pass this to MyFrame so that it can make use of it
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()
Store this MyTools's object in you MyFrame class. And then use this object to call the corresponding method inside MyTools
#ef_UI.py
import wx
''' very simple interface with only a button and a TextCtrl '''
class MyFrame(wx.Frame):
def __init__(self, efToolObj):
wx.Frame.__init__(self, None, title="Menu Test")
self.panel = wx.Panel(self)
self.efToolObj = efToolObj # save the MyTools object to be used later
self.MyButton = wx.Button(self.panel, -1, "Button_test", (0, 0))
self.MyButton.Bind(wx.EVT_BUTTON, self.onClickEvent) # Bind the click event to an event handling method
self.MyTextCtrl = wx.TextCtrl(self.panel, -1, value="just a test", pos=(100, 0))
def onClickEvent(self,event): #this is called when a button is clicked
res = self.efToolObj.OnClickPrinting(self.MyTextCtrl.GetValue()) #Use the mytools object to call its method to apply logic,also get result values
self.MyTextCtrl.SetValue(res) #use the result values in your UI
You can pass the info you want to send to the application logic in its arguments and get results as return values.
#ef_Tool.py
class MyTools:
def OnClickPrinting(self,textvalue):
#Value = self.MyTextCtrl.GetValue()
print "it is working! ",textvalue
resultstr = "test successful"
return resultstr
Hope this helped.
My wxPython GUI either quits with a Segmentation Fault or fails to quit at all using the standard options. The only successful quit option (no errors) is wx.Exit, which I understand is not a great practice. I've traced the issues down to a few factors, but I'm scratching my head as to why they are having this effect.
Using the wxPython inspector (wx.lib.inspection.InspectionTool()), I've been able to determine that a FigureFrameWxAgg is being created when I run certain pylab functions (pylab.xticks() is the function that creates it here, but I haven't tracked down every single function that has this effect). I don't know what this window is for. It's invisible and doesn't appear to do anything. However, this window totally messes up the shutdown of my GUI. If I use self.Destroy, Python doesn't shut down fully. If I use sys.exit, I get a Segmentation fault. I need to catch the wx.EVT_CLOSE so that I can prompt the user to save his/her work.
Here is the code for a simplified version of the GUI:
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
import wx
import wx.grid
import wx.lib.scrolledpanel
import wx.lib.inspection
import sys
import pylab
class my_frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Many Rows')
self.InitUI()
def InitUI(self):
self.panel = wx.Window(self, size=(200, 200))
hbox_all= wx.BoxSizer(wx.HORIZONTAL)
self.create_menubar()
self.fig1 = pylab.Figure((5, 5), dpi=100)
self.canvas1 = FigCanvas(self.panel, -1, self.fig1)
self.fig1.text(0.01,0.98,"Arai plot",{'family':'Arial', 'fontsize':10, 'style':'normal','va':'center', 'ha':'left' })
self.araiplot = self.fig1.add_axes([0.1,0.1,0.8,0.8])
self.araiplot.clear()
self.araiplot.plot(range(5),range(5),lw=0.75,clip_on=False)
xt = pylab.xticks()
grid = wx.grid.Grid(self.panel)
grid.ClearGrid()
grid.CreateGrid(100, 100)
grid.AutoSize()
hbox_all.AddSpacer(self.canvas1)
hbox_all.AddSpacer(20)
hbox_all.AddSpacer(grid)
hbox_all.AddSpacer(20)
self.panel.SetSizer(hbox_all)
hbox_all.Fit(self)
self.Centre()
self.Show()
def create_menubar(self):
"""
Create menu bar
"""
self.menubar = wx.MenuBar()
menu_file = wx.Menu()
menu_file.AppendSeparator()
m_exit = menu_file.Append(wx.ID_EXIT, "Quit", "Quit application")
self.Bind(wx.EVT_CLOSE, self.on_menu_exit)
self.menubar.Append(menu_file, "&File")
self.SetMenuBar(self.menubar)
def on_menu_exit(self, event):
self.Destroy() # this doesn't quit Python fully, unless I comment out 'matplotlib.use('WXAgg')'
#for w in wx.GetTopLevelWindows():
# if w.Title == 'Figure 1':
# w.Destroy() # if I pre-destroy the FigureFrameWxAgg window, I get a PyDeadObjectError when I run self.Destroy
# self.Destroy() #
# wx.Exit() # forces the program to exit, with no clean up. works, but not an ideal solution
#sys.exit() # program closes, but with segmentation error
#self.Close() # creates infinite recursion error, because we have a binding to wx.EVT_CLOSE
if __name__ == '__main__':
app = wx.PySimpleApp(redirect=False)
app.frame = my_frame()
if '-i' in sys.argv:
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
To add one more level of complexity, the Segmentation Fault with sys.exit() only happens with my brew installed Python. Sys.exit() works fine with Canopy Python.
My questions are: how can I fix this error? And, is using wx.Exit() really so bad?
There are several issues with your example:
Do not use pylab in GUI applications, because is brings its own mainloop (which will not quit when the wxPython mainloop quits). You had to kill pylab.
# both not required
# matplotlib.use('WXAgg')
# import pylab
# use instead
from matplotlib.figure import Figure
...
def __init__(# ...
...
self.fig1 = Figure((5, 5), dpi=100)
Your menu item "Close" does not work (at least not on Windows). wx.ID_EXIT is meant for buttons in dialogs. Do not ask me which predefined IDs are meant for menus.
ID_QUIT = wx.NewId()
menu_file.Append(ID_QUIT , "Quit", "Quit application")
#
self.Bind(wx.EVT_MENU, self.on_quit, id=ID_QUIT)
def on_quit(self, evt):
self.Close()
In this case it is not necessary to bind to wx.EVT_CLOSE. If you want to do something on the close event, you have to skip it. When you skip it, wxPython will deal with it on its own.
self.Bind(wx.EVT_CLOSE, self.on_close)
...
def on_close(self, evt):
# you can veto the close here or perform cleanup
evt.Skip()
If you change your code accordingly, wxPython will close everything properly.
I agree with the answer from #nepix32 on not using pylab but use the OOP approach of Matplotlib. However, in my case, I still need the matplotlib.use('WX') for the application to show.
I have the same problem of wxpython GUI application not terminated properly if a Matplotlib figure is displayed even if I'm not using pylab or pyplot.
My workaround is to pass the app object to the wx.Frame class and call app.Exit at EVT_CLOSE.
class my_frame(wx.Frame):
def __init__(self, app=None):
...
self.InitUI()
self.Bind(wx.EVT_CLOSE, lambda evt: app.Exit())
...
if __name__ == '__main__':
...
app.frame = my_frame(app)
...
I am up for anyone who can suggest cleaner solution. To be honest, I'm still new to wxpython.
I am working with python v2.7 and wxPython v3.0 on Windows 7 OS.
In my app I have a about menu. Upon clicking the about menu I want to display some information about my app. I am trying to create a dialog box/AboutBox exactly as shown in the image below.(This is the about dialog of notepad++. Click on ? in the menu bar of notepad++.)
The special thing about the dialog box of notepad++ is that I need a text control window too. One can copy the info.
I tried to do the same in wxPython, but unfortunately I failed. I tried two different hit and trial approaches.
1. I tried to add the text control window to the dialog box wxMessageDialog but it doesn't shows up at all.
2. I tried to use the AboutBox in wxPython, and tried to add the text control to it but it failed because the AboutDialogInfo is not a window and the parent of the text control should be of a window type.
Error:
aboutPanel = wx.TextCtrl(info, -1, style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_controls.py", line 2019, in __init__
_controls_.TextCtrl_swiginit(self,_controls_.new_TextCtrl(*args, **kwargs))
TypeError: in method 'new_TextCtrl', expected argument 1 of type 'wxWindow *'
It would be great if someone could provide some idea on how to add a text control windows to a dialog box/AboutBox?
Code: Here is my code sample for playing around:
import wx
from wx.lib.wordwrap import wordwrap
class gui(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self,None, id, title, style=wx.DEFAULT_FRAME_STYLE)
panel1 = wx.Panel(self, -1)
panel1.SetBackgroundColour('#fffaaa')
menuBar = wx.MenuBar()
file = wx.Menu()
file.Append(101, '&About1', 'About1')
file.Append(102, '&About2', 'About2')
menuBar.Append(file, '&File')
self.SetMenuBar(menuBar)
wx.EVT_MENU(self, 101, self.onAbout)# Event for the About1 menu
wx.EVT_MENU(self, 102, self.onAboutDlg)# Event for the About2 menu
def onAbout(self, event):
message = 'This fantastic app was developed using wxPython.\nwxPython is c00l :)'
dlg = wx.MessageDialog(self, message, 'My APP', wx.OK|wx.ICON_INFORMATION)
aboutPanel = wx.TextCtrl(dlg, -1, style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
aboutPanel.WriteText('Experimentation is the part of our life.\n')
dlg.ShowModal()
dlg.Destroy()
def onAboutDlg(self, event):
self.panel = wx.Panel(self, -1)
info = wx.AboutDialogInfo()
info.Name = "My About Box"
info.Version = "0.1"
info.Copyright = "(C) 2014 xxx"
info.Description = wordwrap(
"This is an example application that shows the problem "
"that I am facing :)",
350, wx.ClientDC(self.panel))
info.WebSite = ("http://stackoverflow.com/users/2382792/pss", "My Home Page")
info.Developers = ["PSS"]
info.License = wordwrap("Driving license and a AK-47 too :P ", 500,wx.ClientDC(self.panel))
# Uncomment the following line to get the error!
#aboutPanel = wx.TextCtrl(info, -1, style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
#aboutPanel.WriteText('Experimentation is the part of our life.\n')
wx.AboutBox(info)
if __name__ == '__main__':
app = wx.App()
frame = gui(parent=None, id=-1, title="My-App")
frame.Show()
app.MainLoop()
Thank you for your time!
wxAboutBox() uses the standard about dialog for the current platform so it won't help you to achieve your goal. Similarly, wxMessageDialog is the native message box dialog which, again, can't have your custom text box. OTOH there is absolutely no problem with building any dialog you want using wxDialog and adding elements to it (and using sizers to lay them out).
IOW your mistake is to think that the class you need to use is wxMessageDialog: it isn't, you need wxDialog.
Is it possible to run a wxApp from another wxApp?
I am trying to simply call a program I wrote (called DataDeck) from a method of another wxApp, like it was a plugin.
something like:
def on_datadeck_btn_click(self, event):
import datadeck.main
datadeck.main.run()
event.Skip()
where datadeck.main.run() is a classic start of a wxApp:
def run():
app = DataDeck(0)
app.SetAppName("DataDeck")
app.MainLoop()
Right now, it correctly opens DataDeck the first time and it works, but it won't reopen DataDeck a second time after I close it. This would freeze everything.
Update: based on #Mike Driscoll answer, I documented myself more and came to the following solution:
I added an "entry point" in datadeck
def run_as_plugin():
#[do some stuff related to XRC layouts and sysout redirection]
MainGUI = datadeck.gui.maingui.MainGUI()
Where the constructor of MainGUI() automatically shows the wxFrame. Now my application behaves like it was a component of the caller wxApp.
Therefore, I modify the application method as follows:
def on_datadeck_btn_click(self, event):
import datadeck.main
datadeck.main.run_as_plugin()
event.Skip()
It was very simple, indeed! I just had to modify my objects that deal with stdout redirection (not part of this question, I omit the details), and everything worked fine.
There should only be on wx.App. From what I've read online, you can't have two wx.App objects running in one script. You could probably do it using the subprocess module to open a new process though. Take a look at Editra to see some examples for how to do plugins. It is included with wxPython or you can download it separately.
It is perfectly feasible. Not sure why it doesnt work for you.
This example works perfectly:
--main.py--
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title='Main', size=(353,270))
button= wx.Button(self, -1, 'call app', pos=(10,10), size=(-1,30))
self.Bind(wx.EVT_BUTTON, self.capp, button)
def capp(self, event):
import datadeck
datadeck.run()
if __name__ == '__main__':
app = wx.App(0)
frame = MainFrame(None)
frame.Show()
app.MainLoop()
--datadeck.py--
import wx
class DDFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title='DDFrame', size=(353,270))
button = wx.Button(self, -1, 'print something', pos=(100,100), size=(-1,30))
self.Bind(wx.EVT_BUTTON, self.say_hello, button)
def say_hello(self, event):
print 'something'
class DataDeck(wx.App):
def OnInit(self):
frame = DDFrame(None)
frame.Show()
return True
def run():
app = DataDeck(1)
app.SetAppName("DataDeck")
app.MainLoop()
if you press the 'call app' button you get the new frame open. And you can open as many as you want.
Created aplications/frames are independent of each other. You can close any of them without affecting the others. And the system doesnt freeze.
I'm using a wxPython listbox on Windows to get a choice from the user, and I would like them to be able to select an item using the ENTER key, as if they had double-clicked. I know how to do this in C or C++ using the Windows API directly, but can't seem to find how to do it using wxPython. Anyone know how? It seems like an obvious thing to want to do.
Maybe I'm missing some nuance, there wasn't much info to go on, but it sounds like you could accomplish this by catching the keydown event, matching for enter and then calling your on_doubleclick function. Unless there's an implicit double-click handling you should be good to go.
The simple answer is that the wx.ListBox doesn't support that. Try using a one column wx.ListCtrl (in Report mode) instead.
This works for me, the key is to Bind EVT_KEY_UP, rather than EVT_KEY_DOWN:
import wx
class myListBoxDemo(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent, title="testing ListBox right-click")
self.panel = wx.Panel(self, -1)
sizer = wx.BoxSizer(wx.VERTICAL)
self.myListBox = wx.ListBox(self.panel,style=wx.LB_HSCROLL|wx.LB_SINGLE)
self.myListBox.Bind(wx.EVT_KEY_UP, self.OnKeyDownSelectedList)
self.myListBox.Append('item 1')
self.myListBox.Append('item 2')
self.myListBox.Append('item 3')
self.myListBox.Append('item 4')
sizer.AddF(self.myListBox, wx.SizerFlags(1).Expand())
self.panel.SetSizer(sizer)
def OnKeyDownSelectedList(self, event):
print "KeyCode: %d" % event.GetKeyCode()
print "ListBox Item Index: %d" % event.GetEventObject().GetSelection()
import wx.lib.mixins.inspection as wit
class AppWInspection(wx.App, wit.InspectionMixin):
def OnInit(self):
self.Init() # enable Inspection tool
return True
if __name__ == "__main__":
app = AppWInspection()
f = myListBoxDemo(None)
f.Show()
app.MainLoop()