Tix ComboBox 导致 python2.7 崩溃

发布于 2024-10-18 00:27:39 字数 2044 浏览 1 评论 0原文

我一直在使用 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 吗?

++++++++++++++++++++++++++++++

好的,我设法通过添加 value =' ' 来对此进行猴子修补,这样返回字符串不为空,然后当我需要时 lstrip() 值。

self.combo = Tix.ComboBox(window, value =' ')
...
tkMessageBox.showinfo('info', self.combo['selection'].lstrip())

非常丑陋,但现在可以阻止我崩溃;直到有更聪明的人能给我更好的答案。 如果用户删除该空格然后按“开始”,它肯定会再次导致崩溃!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-10-25 00:27:39

我不是 Tix 专家,但这应该可以解决问题:

tkMessageBox.showinfo('info', self.combo.entry.get())

在字里行间,我建议使用 wxPython学习GUI编程。作为一个学习环境,这对我来说非常棒。

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文