Tix ComboBox causes python2.7 to crash
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 开发者_如何学Gobe 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()
精彩评论