通用定義對話框

对话框dialog — 通用预定义对话框common predefined dialogs

wxPython 提供了几个通用对话框. 这为程序员节省了大量劳动. 也能令到应用程序更为符合有关用户界面的各种标准. 我们将展示以下一些通用对话框:[more…]

  • wx.MessageDialog
  • wx.ColourDialog
  • wx.PageSetupDialog
  • wx.FontDialog
  • wx.DirDialog
  • wx.SingleChoiceDialog
  • wx.TextEntryDialog

 

#!/usr/bin/python
#coding=utf-8

#commondialogs.py

import wx
import os, sys

class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)

statusBar = self.CreateStatusBar()
statusBar.SetMinHeight(100)
menuBar= wx.MenuBar()
menu = wx.Menu()
menu.Append(99, "&Message Dialog", "Shows a Message Dialog")
menu.Append(100, "&Colour Dialog", "Shows a Color Dialog")
menu.Append(101, "&File Dialog", "Shows a File Dialog")
menu.Append(102, "&Page Setup Dialog", "Shows a Page Setup Dialog")
menu.Append(103, "F&ont Dialog", "Shows a Font Dialog")
menu.Append(104, "D&irectory Dialog", "Shows a Directory Dialog")
menu.Append(105, "&SingleChoice Dialog", "Shows a SingleChoice Dialog")
menu.Append(106, "&TextEntry", "Shows a TextEntry Dialog")
menu.Append(107, "&Quit", "退出程序")
menuBar.Append(menu, "&Dialogs")
self.SetMenuBar(menuBar)

self.Bind(wx.EVT_MENU, self.message, id=99)
self.Bind(wx.EVT_MENU, self.choosecolor, id=100)
self.Bind(wx.EVT_MENU, self.openfile, id=101)
self.Bind(wx.EVT_MENU, self.pagesetup, id=102)
self.Bind(wx.EVT_MENU, self.choosefont, id=103)
self.Bind(wx.EVT_MENU, self.opendir, id=104)
self.Bind(wx.EVT_MENU, self.singlechoice, id=105)
self.Bind(wx.EVT_MENU, self.textentry, id=106)
self.Bind(wx.EVT_MENU, self.quit, id=107)

def message(self, event):
dlg = wx.MessageDialog(self, 'To save one life is as if you have save the world.',
'Talmud', wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()

    def choosecolor(self, event):
        dlg = wx.ColourDialog(self)
        dlg.GetColourData().SetChooseFull(True)
        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetColourData()
            self.SetStatusText('You Selected: %s' % str(data.GetColour().Get()))
            
        dlg.Destroy()
        
    def openfile(self, event):
        dlg = wx.FileDialog(self, "Choose a file: ", os.getcwd(), "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            mypath = os.path.basename(path)
            self.SetStatusText("You selected: %s" % mypath)
            
        dlg.Destroy()
        
    def pagesetup(self, event):
        dlg = wx.PageSetupDialog(self)
        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetPageSetupData()
            tl = data.GetMarginTopLeft()
            br = data.GetMarginBottomRight()
            self.SetStatusText('Margins are: %s %s ' % (str(tl), str(br)))
            
        dlg.Destroy()

    def choosefont(self, event):
        default_font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, False, "Verdana")
        data = wx.FontData()
        if sys.platform == 'win32':
            data.EnableEffects(True)
        data.SetAllowSymbols(False)
        data.SetInitialFont(default_font)
        data.SetRange(10, 30)
        dlg = wx.FontDialog(self, data)
        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetFontData()
            font = data.GetChosenFont()
            color = data.GetColour()
            text = 'Face: %s, Size: %s, Color: %s' % (font.GetFaceName(), font.GetPointSize(), color.Get())
            self.SetStatusText(text)
            
        dlg.Destroy()
        
    def opendir(self, event):
        dlg = wx.DirDialog(self, "Choose a directory: ", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dlg.ShowModal() == wx.ID_OK:
            self.SetStatusText('You selected: %s' % dlg.GetPath())
            
        dlg.Destroy()
        
        
    def singlechoice(self, event):
        sins = ['Greed', 'Lust', 'Gluttony', 'Pride', 'Sloth', 'Envy', 'Wrath']
        dlg = wx.SingleChoiceDialog(self, 'Seven Deadly sin: ', 'Which one?', sins, wx.CHOICEDLG_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            self.SetStatusText('You choose: %s' % dlg.GetStringSelection())
            
        dlg.Destroy()

    def textentry(self, event):
        dlg = wx.TextEntryDialog(self, 'Enter some text', 'Text Entry')
        dlg.SetValue('Default')
        if dlg.ShowModal() == wx.ID_OK:
            self.SetStatusText('You entered: %s' % dlg.GetValue())
            
        dlg.Destroy()
        
    def quit(self, event):
        dlg = self.Close()
        
class MyApp(wx.App):
    def OnInit(self):
        myframe = MyFrame(None, -1, 'commondialogs.py')
        myframe.CenterOnScreen()
        myframe.Show(True)
        return True
    
app = MyApp(0)
app.MainLoop()

这段程序展示了 8 种不同的通用对话框. 所有输出都显示在状态栏上.

f-13
f-14
f-15
f-16
f-17
f-18
f-19
f-20

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值