题
我一直在使用tix来创建一个组合框,如果输入框留空,则会导致间歇性崩溃。
我是python的新手,非常新的gui编程,所以我一直在使用榜样来教授自己的东西。
当使用以下示例代码时,您应该能够在条目框中输入值,或者选择表单下拉菜单,但是如果您将条目字段留空并按Go,它将导致Python崩溃。
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()
.
========================= 崩溃细节:
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
.
所以
a]是一个例子是错误的吗?
b]有更好的方法吗?
c]我应该使用tix吗?
我注意到有些人似乎远离tix,只使用tkinter。是否有一个原因?我应该不使用tix吗?
++++++++++++++++++++++++++++
好的,所以我通过添加值=''来扫描它,因此返回字符串不为空,然后lip()当我需要它时值。
.
self.combo = Tix.ComboBox(window, value =' ')
...
tkMessageBox.showinfo('info', self.combo['selection'].lstrip())
非常丑陋,但现在阻止我崩溃;直到有人更聪明地给我一个更好的答案。 如果用户删除了空间,然后按“GO”,它会再次导致崩溃!
解决方案
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()