wxpython应用程序错误_WXPython错误处理和重复使用

我有一个wxpython应用程序,当您单击某个按钮时,它会对特定输入执行一系列步骤。应用程序第一次运行,但是如果我尝试用不同的输入值再次单击按钮,它会抛出一个错误。在

我有两个问题:

1.)如何修复下面的代码,使其停止抛出此错误消息?在

2.)我应该添加哪些特定的代码,以便在对话框中向最终用户显示类似此错误消息?我希望它能够优雅地终止抛出错误的整个进程(从单击按钮开始),并用一个新的对话框替换进度条,该对话框给出错误消息,并允许用户单击一个按钮,将他们带回应用程序的主窗口,以便他们可以再次尝试单击按钮它与新的输入一起工作。我很快就要把它打包成一个exe文件,但是现在我唯一的错误处理方法就是pythonshell中的错误消息。由于用户没有pythonshell,所以我需要以本段中描述的方式显示错误消息。注意,这里的OnClick()函数实例化了一个类。我在实际代码中使用了许多不同的类,每个类都非常复杂,我需要这里所要求的功能,以便能够处理由OnClick()函数实例化的各种类的内部深处发生的错误。在

下面是我的代码的一个非常简化但有效的版本:import wxversion

import wx

ID_EXIT = 130

class MainWindow(wx.Frame):

def __init__(self, parent,id,title):

wx.Frame.__init__(self,parent,wx.ID_ANY,title, size = (500,500), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)

# A button

self.button =wx.Button(self, label="Click Here", pos=(160, 120))

self.Bind(wx.EVT_BUTTON,self.OnClick,self.button)

# the combobox Control

self.sampleList = ['first','second','third']

self.lblhear = wx.StaticText(self, label="Choose TestID to filter:", pos=(20, 75))

self.edithear = wx.ComboBox(self, pos=(160, 75), size=(95, -1), choices=self.sampleList, style=wx.CB_DROPDOWN)

# the progress bar

self.progressMax = 3

self.count = 0

self.newStep='step '+str(self.count)

self.dialog = None

#-------Setting up the menu.

# create a new instance of the wx.Menu() object

filemenu = wx.Menu()

# enables user to exit the program gracefully

filemenu.append(ID_EXIT, "E&xit", "Terminate the program")

#------- Creating the menu.

# create a new instance of the wx.MenuBar() object

menubar = wx.MenuBar()

# add our filemenu as the first thing on this menu bar

menubar.Append(filemenu,"&File")

# set the menubar we just created as the MenuBar for this frame

self.SetMenuBar(menubar)

#----- Setting menu event handler

wx.EVT_MENU(self,ID_EXIT,self.OnExit)

self.Show(True)

def OnExit(self,event):

self.Close(True)

def OnClick(self,event):

#SECOND EDIT: added try: statement to the next line:

try:

if not self.dialog:

self.dialog = wx.ProgressDialog("Progress in processing your data.", self.newStep,

self.progressMax,

style=wx.PD_CAN_ABORT

| wx.PD_APP_MODAL

| wx.PD_SMOOTH)

self.count += 1

self.newStep='Start'

(keepGoing, skip) = self.dialog.Update(self.count,self.newStep)

TestID = self.edithear.GetValue()

self.count += 1

self.newStep='Continue.'

(keepGoing, skip) = self.dialog.Update(self.count,self.newStep)

myObject=myClass(TestID)

print myObject.description

self.count += 1

self.newStep='Finished.'

(keepGoing, skip) = self.dialog.Update(self.count,self.newStep)

**#FIRST EDIT: Added self.count = 0 on the following line**

self.count = 0

self.dialog.Destroy()

#SECOND EDIT: Added the next seven lines to handle exceptions.

except:

self.dialog.Destroy()

import sys, traceback

xc = traceback.format_exception(*sys.exc_info())

d = wx.MessageDialog( self, ''.join(xc),"Error",wx.OK)

d.ShowModal() # Show it

d.Destroy() #finally destroy it when finished

class myClass():

def __init__(self,TestID):

self.description = 'The variable name is: '+str(TestID)+'. '

app = wx.PySimpleApp()

frame = MainWindow(None,-1,"My GUI")

app.MainLoop()

我的实际代码要复杂得多,但是我创建了这个虚拟版本来说明其中的问题。当我第一次单击按钮时,上面的代码可以在python中运行。但如果我再次尝试单击该按钮,则在python shell中会收到以下错误消息:

^{pr2}$

第一次编辑:好的,我通过添加self.count = 0

以上self.dialog.Destroy()

在上面的代码中。但是关于上面代码中错误处理的第二个问题呢?有人能帮我吗?当然,wxpython中的错误处理是一个常见的问题,有一些已知的方法。我将使用py2exe将这个应用程序打包成一个exe文件。在

第二次编辑:我用try:回答了上面的第二个问题,除了:我在上面的代码中添加了语句。有趣的是,这是第一次堆栈溢出上没有人能够回答我的问题。我对这种类型的编程还不熟悉,所以很多事情我都是顺其自然的。这个问题现在得到了回答。在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的用 wxPython 实现登录界面和界面跳转的示例代码: ```python import wx class LoginPanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent) self.username_label = wx.StaticText(self, label='Username:', pos=(50, 50)) self.username_text = wx.TextCtrl(self, pos=(150, 50)) self.password_label = wx.StaticText(self, label='Password:', pos=(50, 100)) self.password_text = wx.TextCtrl(self, style=wx.TE_PASSWORD, pos=(150, 100)) self.login_button = wx.Button(self, label='Login', pos=(100, 150)) self.login_button.Bind(wx.EVT_BUTTON, self.on_login) def on_login(self, event): username = self.username_text.GetValue() password = self.password_text.GetValue() if username == 'admin' and password == 'admin': frame.switch_panel(LoginPanel, WelcomePanel) else: wx.MessageBox('Invalid username or password', 'Error', wx.OK | wx.ICON_ERROR) class WelcomePanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent) welcome_label = wx.StaticText(self, label='Welcome!', pos=(50, 50)) class MainFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, title='Login System', size=(300, 250)) self.Center() self.panel_dict = {} self.login_panel = LoginPanel(self) self.switch_panel(None, LoginPanel) def switch_panel(self, old_panel, new_panel_class): if old_panel: old_panel.Destroy() if new_panel_class in self.panel_dict: new_panel = self.panel_dict[new_panel_class] else: new_panel = new_panel_class(self) self.panel_dict[new_panel_class] = new_panel new_panel.Show() if __name__ == '__main__': app = wx.App() frame = MainFrame() frame.Show() app.MainLoop() ``` 在这个示例中,我们定义了两个面板:登录界面 `LoginPanel` 和欢迎界面 `WelcomePanel`。在登录界面中,我们添加了一个用户名输入框、一个密码输入框和一个登录按钮。当用户点击登录按钮时,我们获取输入的用户名和密码,如果用户名和密码都为 `admin`,则切换到欢迎界面;否则,弹出一个错误提示框。 我们还定义了一个 `MainFrame` 类,它继承自 `wx.Frame`,用于管理面板的切换。在 `MainFrame` 中,我们使用一个字典来保存已经创建的面板实例,这样可以避免重复创建。我们还定义了一个 `switch_panel` 方法,用于切换面板。当需要切换到一个新的面板时,我们先销毁旧面板,然后创建或从字典中获取新面板实例,最后显示新面板。 在 `MainFrame` 的 `__init__` 方法中,我们创建了 `LoginPanel` 面板并显示。在 `LoginPanel` 的 `on_login` 方法中,如果用户名和密码都为 `admin`,则调用 `frame.switch_panel` 方法将当前面板切换到 `WelcomePanel`。 通过这个示例,你可以了解到如何使用 wxPython 实现界面跳转。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值