用wxpython 5行代码显示一个对话框

书上的方法都是先自定义Frame类,然后定义__init__函数,再自定义App类和OnInit函数,所以比较复杂。

我是直接实例化wxpython的App类和Frame框架,当然,如果要在框中添加再复杂一点的功能,最好就自定义Frame类。


#!/usr/bin/python

import wx

VarifyApp = wx.App()
VarifyApp.Frame = wx.Frame(None,-1,title="HelloWorld")
VarifyApp.Frame.Show()
VarifyApp.MainLoop()        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selecting the "Horizontal Scroll" and "Verticla Scroll" styles among the properties of your dialog box in the resource editor, you can add scroll bars to the dialog box. Remember also to select the 'resizing' border style. However for adding functionality to the scroll bars, you need to override the WM_VSCROLL and WM_HSCROLL message handlers. Also,override the WM_SIZE handler to set the scroll bar range if the size is reduced than the original. So you get the original size of the dialog in your OninitDialog(). The code would look something like this. Modify to your needs. 1. To OnInitDialog(),add the following line. GetWindowRect(m_rect); m_nScrollPos = 0; to get the original window size. Make m_rect a member variable of your dialog. Add another variable m_nScrollPos and initialize its value to zero. It stores the current vertical scroll position. 2. Here is the WM_SIZE handler for setting the scroll bar range.Set range 0 if size is increased more than original. void CCharlesDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: Add your message handler code here m_nCurHeight = cy; int nScrollMax; if (cy < m_rect.Height()) { nScrollMax = m_rect.Height() - cy; } else nScrollMax = 0; SCROLLINFO si; si.cbSize = sizeof(SCROLLINFO); si.fMask = SIF_ALL;// SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS; si.nMin = 0; si.nMax = nScrollMax; si.nPage = si.nMax/10; si.nPos = 0; SetScrollInfo(SB_VERT, &si, TRUE); } You need m_nCurHeight to store the current height of the dialog and use it to handle the scrolling in OnVScroll. m_ncurHeight is also a member variable of the dialog. 3. Here is the handler for WM_VSCROLL. void CCharlesDlg::OnVScroll(UINT nSBCode,UINT nPos,CScrollBar* pScrollBar) { //TODO:Add your message handler code here and/or call default int nDelta; int nMaxPos = m_rect.Height() - m_nCurHeight; switch(nSBCode) { case SB_LINEDOWN: if (m_nScrollPos >= nMaxPos) return; nDelta = min(nMaxPos/100,nMaxPos-m_nScrollPos); break; case SB_LINEUP: if (m_nScrollPos <= 0) return; nDelta = -min(nMaxPos/100,m_nScrollPos); break; case SB_PAGEDOWN: if (m_nScrollPos >= nMaxPos) return; nDelta = min(nMaxPos/10,nMaxPos-m_nScrollPos); break; case SB_THUMBPOSITION: nDelta = (int)nPos - m_nScrollPos; break; case SB_PAGEUP: if (m_nScrollPos <= 0) return; nDelta = -min(nMaxPos/10,m_nScrollPos); break; default: return; } m_nScrollPos += nDelta; SetScrollPos(SB_VERT,m_nScrollPos,TRUE); ScrollWindow(0,-nDelta); CDialog::OnVScroll(nSBCode, nPos, pScrollBar); } The above code handles the vertical scrolling. For horizontal scrolling add the WM_HSCROLL similarly and add the necessary code to OnSize and OnInitDialog. Information provided in this document and any software that may accompany this document is provided "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The user assumes the entire risk as to the accuracy and the use of this information.
### 回答1: 好的,我可以回答这个问题。使用wxPython编写记事本程序可以通过以下步骤实现: 1. 导入wxPython模块 2. 创建一个应用程序对象 3. 创建一个顶级窗口对象 4. 在窗口对象中添加文本框控件 5. 在窗口对象中添加菜单栏和工具栏 6. 实现菜单栏和工具栏的功能,例如打开、保存、剪切、复制、粘贴等 7. 运应用程序 以上是大致的步骤,具体实现可以参考wxPython的官方文档和示例代码。 ### 回答2: 用wxPython编写一个记事本程序可以实现基本的文本编辑功能。首先,我们需要导入wxPython模块,并创建一个应用程序对象和一个顶层窗口对象。然后,在窗口对象中添加一个文本框作为编辑区域,并设置其样式和布局。接下来,我们可以添加菜单栏和工具栏来增加一些功能按钮,如新建、打开、保存、剪切、复制、粘贴等等。通过绑定事件处理函数,我们可以实现这些按钮的功能。 例如,当点击新建按钮时,我们可以清空文本框中的内容;当点击打开按钮时,可以弹出文件选择对话框,选择要打开的文件并将其内容显示在文本框中;当点击保存按钮时,可以弹出保存文件对话框,将文本框中的内容保存到指定的文件中。 除了基本的编辑功能,我们还可以通过wxPython提供的丰富控件库来增加其他功能,如查找和替换、字体和颜色设置、撤销和重做等等。 总之,使用wxPython编写记事本程序可以使我们能够方便地创建一个简单的文本编辑工具,通过定制和扩展,我们还可以实现更多更高级的功能。 ### 回答3: 使用wxPython编写一个记事本程序,首先需要导入wxPython模块。然后创建一个wx.App()实例来启动程序。 接下来,创建一个wx.Frame()实例作为主窗口。在主窗口中,可以添加一个wx.TextCtrl()组件作为文本输入框,用来输入和编辑文字内容。还可以添加一个菜单栏和工具栏,用来实现一些额外的功能比如打开、保存文件,设置字体、样式等。 在编写记事本程序时,可以定义相应的事件处理函数来实现功能。比如,可以为菜单栏和工具栏上的“打开”按钮绑定一个事件处理函数,当用户点击这个按钮时,就会触发这个函数,从而实现打开文件的功能。类似地,可以为保存按钮绑定一个事件处理函数,实现保存文件的功能。 此外,还可以添加一些其他的功能,比如设置字体样式、查找和替换文本、撤销和重做操作等。可以通过定义相应的事件处理函数来监听用户的操作,实现这些功能。 最后,调用wx.App().MainLoop()来进入主事件循环,使程序能够响应用户的操作,并持续运。 这个记事本程序使用wxPython编写,可以在图形界面下方便地输入和编辑文字内容,并提供了一些常用的功能如打开、保存文件,设置字体样式等。可以根据实际需求,进一步扩展和优化这个记事本程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值