wxPython的:如何绑定按钮? [英] wxPython: How to bind the button?

查看:151
本文介绍了wxPython的:如何绑定按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始用的wxPython并与绑定的问题。

通常我找到一个按钮事件绑定的例子与 self.Bind完成(wx.EVT_BUTTON,self.OnWhatEverFunction,按钮)。我有一个内部的面板和按钮,我想什么都一个框架,结局总是出现在一秒钟闪烁的框架,就是这样。由于code是没有那么多我在这里附上它,希望​​你们可以告诉我了我的小问题的方法。

在此先感谢,
托马斯

#!的/ usr / bin中/蟒蛇
进口WX类CPFSFrame(wx.Frame):
    高清__init __(自我,父母,标题):
        超(CPFSFrame,个体经营).__的init __(父母,标题=标题,
                                    风格= wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
                                    大小=(350,200))
        面板= wx.Panel(个体经营,-1)        pNumberLabel = wx.StaticText(面板,-1,项目编号:')
        pNumberText = wx.TextCtrl(面板,-1,'',大小=(175,-1))
        pNumberText.SetInsertionPoint(0)        pNameLabel = wx.StaticText(面板,-1,项目名称:)
        pNameText = wx.TextCtrl(面板,-1,'',大小=(175,-1))
        pNameText.SetInsertionPoint(0)        pButton = wx.Button(面板,标签='创建')
        pButton.Bind(wx.EVT_BUTTON,self.OnCreate)        分级机= wx.FlexGridSizer(COLS = 2,hgap指定= 6,vgap = 6)
        sizer.AddMany([pNumberLabel,pNumberText,pNameLabel,pNameText,pButton])
        panel.SetSizer(分级机)        的状态栏。= self.CreateStatusBar()    高清的OnCreate(个体经营,EVT):
        self.Close(真)        self.Centre()
        self.Show()如果__name__ =='__main__':
    应用= wx.App()
    CPFSFrame(无,标题='创建新项目的文件夹结构)
    app.MainLoop()


解决方案

您需要把self.Show()方法在的init 的code部分。起初我还以为OnCreate()中正在运行,如果是的话,那就只是关闭框立刻让你不会看到任何东西。我反而把它的OnClose。这里有一个工作的例子:

进口WX类CPFSFrame(wx.Frame):
    高清__init __(自我,父母,标题):
        超(CPFSFrame,个体经营).__的init __(父母,标题=标题,
                                    风格= wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
                                    大小=(350,200))
        面板= wx.Panel(个体经营,-1)        pNumberLabel = wx.StaticText(面板,-1,项目编号:')
        pNumberText = wx.TextCtrl(面板,-1,'',大小=(175,-1))
        pNumberText.SetInsertionPoint(0)        pNameLabel = wx.StaticText(面板,-1,项目名称:)
        pNameText = wx.TextCtrl(面板,-1,'',大小=(175,-1))
        pNameText.SetInsertionPoint(0)        pButton = wx.Button(面板,标签='创建')
        pButton.Bind(wx.EVT_BUTTON,self.OnClose)        分级机= wx.FlexGridSizer(COLS = 2,hgap指定= 6,vgap = 6)
        sizer.AddMany([pNumberLabel,pNumberText,pNameLabel,pNameText,pButton])
        panel.SetSizer(分级机)        的状态栏。= self.CreateStatusBar()
        self.Show()    高清的OnClose(个体经营,EVT):
        self.Close(真)如果__name__ =='__main__':
    应用= wx.App()
    CPFSFrame(无,标题='创建新项目的文件夹结构)
    app.MainLoop()

I just started with wxPython and have problems with the binding.

Usually the examples I find to bind a button event are done withself.Bind(wx.EVT_BUTTON, self.OnWhatEverFunction, button). I have a frame with a panel inside and a button and what ever I tried, the outcome is always a flashing frame that appears in a split second and that's it. Since the code isn't that much I attach it here and hope one of you can show me the way out of my little problem.

Thanks in advance, Thomas

#!/usr/bin/python
import wx

class CPFSFrame(wx.Frame):
    def __init__(self, parent, title):
        super(CPFSFrame, self).__init__(parent, title=title,
                                    style = wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
                                    size=(350, 200))
        panel = wx.Panel(self, -1)

        pNumberLabel = wx.StaticText(panel, -1, 'Project number: ')
        pNumberText = wx.TextCtrl(panel, -1, '', size=(175, -1))
        pNumberText.SetInsertionPoint(0)

        pNameLabel = wx.StaticText(panel, -1, 'Project name: ')
        pNameText = wx.TextCtrl(panel, -1, '', size=(175, -1))
        pNameText.SetInsertionPoint(0)

        pButton = wx.Button(panel, label='Create')
        pButton.Bind(wx.EVT_BUTTON, self.OnCreate)

        sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
        sizer.AddMany([pNumberLabel, pNumberText, pNameLabel, pNameText, pButton])
        panel.SetSizer(sizer)

        statusBar = self.CreateStatusBar()

    def OnCreate(self, evt):
        self.Close(True)

        self.Centre()
        self.Show()

if __name__ == '__main__':
    app = wx.App()
    CPFSFrame(None, title='Create New Project Folder Structure')
    app.MainLoop()

解决方案

You need to put the "self.Show()" method in the init section of the code. At first I thought OnCreate() was running and if it was, then it would just close the frame immediately so you wouldn't see anything. I would call it OnClose instead. Here's a working example:

import wx

class CPFSFrame(wx.Frame):
    def __init__(self, parent, title):
        super(CPFSFrame, self).__init__(parent, title=title,
                                    style = wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
                                    size=(350, 200))
        panel = wx.Panel(self, -1)

        pNumberLabel = wx.StaticText(panel, -1, 'Project number: ')
        pNumberText = wx.TextCtrl(panel, -1, '', size=(175, -1))
        pNumberText.SetInsertionPoint(0)

        pNameLabel = wx.StaticText(panel, -1, 'Project name: ')
        pNameText = wx.TextCtrl(panel, -1, '', size=(175, -1))
        pNameText.SetInsertionPoint(0)

        pButton = wx.Button(panel, label='Create')
        pButton.Bind(wx.EVT_BUTTON, self.OnClose)

        sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
        sizer.AddMany([pNumberLabel, pNumberText, pNameLabel, pNameText, pButton])
        panel.SetSizer(sizer)

        statusBar = self.CreateStatusBar()
        self.Show()

    def OnClose(self, evt):
        self.Close(True)

if __name__ == '__main__':
    app = wx.App()
    CPFSFrame(None, title='Create New Project Folder Structure')
    app.MainLoop()

这篇关于wxPython的:如何绑定按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆