wxPython关于对话框

#!/usr/bin/python
#-*- coding:utf8 -*-

#现在讲讲dialog的使用

import wx
import os

class DialogFrame(wx.Frame):
    def __init__(self, root):
        wx.Frame.__init__(self, root, -1, u"对话框",size=(200,200))
        panel = wx.Panel(self, -1)
        self.btn = wx.Button(panel, -1, u"点击", pos=(30,30))

        self.btn.Bind(wx.EVT_BUTTON, self.OnDialog)
        
    def OnDialog(self, event):
        #print "test"
        pos = self.GetPosition()
        dialog = ShowDialog(self, -1, u"演示")
        dialog.SetPosition((pos[0]+100, pos[1]+60))


class ShowDialog(wx.Dialog):
    def __init__(self, root, id, title):
        wx.Dialog.__init__(self,root,id,title,size=(100,100))
        self.panel = wx.Panel(self, -1)
        self.OnBtn = wx.Button(self, 10, u"确定", pos=(10,20), size=(80,40))
        self.Bind(wx.EVT_BUTTON, self.CloseDlg, self.OnBtn)
        self.Show()

    def CloseDlg(self, event):
        self.Close()



#简单对话框判断用户输入
class PutFrame(wx.Frame):
    def __init__(self,root):
        wx.Frame.__init__(self, root, -1, u"选择", size=(300,300))
        button = wx.Button(self, wx.ID_OK, "Exit", pos=(10,10))
        button.SetDefault()
        self.Bind(wx.EVT_BUTTON, self.OnClick, button)
    
    def OnClick(self, event):
        dlg = wx.MessageDialog(None, u"是否退出", u"退出", wx.YES_NO | wx.ICON_QUESTION)
        if(dlg.ShowModal() == wx.ID_YES):
            self.Close()
        dlg.Destroy()



#文件对话框
def FileDialog():
    fileFilter = "Python File(*.py)|*.py | All files(*.*)|*.*"
    fileDlg = wx.FileDialog(None, u"文件选择", os.getcwd(), "", fileFilter, wx.OPEN)
    fileDlg.ShowModal()
    fileDlg.Destroy()



#文本对话框
class TextFrame(wx.Frame):
    def __init__(self, root):
        wx.Frame.__init__(self, root, -1, u"文本对话框", size=(200,200))
        panel = wx.Panel(self, -1)
        print "test1"
        self.textCtrl = wx.TextCtrl(panel,-1,"",pos=(10,10),style=wx.TE_PROCESS_ENTER)
        self.textCtrl.Bind(wx.EVT_TEXT_ENTER, self.OnClick, self.textCtrl)
    
    def OnClick(self, event):
        print "test"
        self.textDlg = wx.TextEntryDialog(None, u"输入文本",u"文本对话框", style=wx.OK|wx.CANCEL)
        if self.textDlg.ShowModal() == wx.ID_OK:
            self.textCtrl.Value = self.textDlg.GetValue()





#到网上还看到一个比较好玩的东西  对话框的验证器
class TestFrame(wx.Frame):
    def __init__(self, root):
        wx.Frame.__init__(self, root, -1, u"验证",size=(300,300))
        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.addOne = wx.TextCtrl(panel,-1,"",pos=(10,10))
        addOperator = wx.StaticText(panel, -1, "+")
        self.addTwo = wx.TextCtrl(panel,-1,"")

        addBtn = wx.Button(panel, -1, u"计算")
        addBtn.Bind(wx.EVT_BUTTON, self.OnClick, addBtn)
        
        sizer.Add(self.addOne)
        sizer.Add(addOperator)
        sizer.Add(self.addTwo)
        sizer.Add(addBtn)
        
        panel.SetSizer(sizer)
        panel.Fit()

    def OnClick(self, event):
        data = {0:self.addOne.GetValue(),1:self.addTwo.GetValue()}
        dlg = ShowDlg(data)
        dlg.ShowModal()
        dlg.Destroy()

class DataValidator(wx.PyValidator):
    def __init__(self, data, key):
        wx.PyValidator.__init__(self)
        self.data = data
        self.key = key

    def Clone(self):
        return DataValidator(self.data, self.key)

    def Validate(self, win):
        print "validate data"
        return True

    def TransferToWindow(self):
        print "open the window"
        textCtrl = self.GetWindow()
        textCtrl.SetValue(self.data.get(self.key, ""))
        return True

    def TransferFromWindow(self):
        print "close the window"
        return True


class ShowDlg(wx.Dialog):
    def __init__(self, data):
        wx.Dialog.__init__(self, None, -1, u"验证")
        staticText1 = wx.StaticText(self, -1, u"数字1")
        staticText2 = wx.StaticText(self, -1, u"数字2")
        
        self.textCtrl1 = wx.TextCtrl(self, validator=DataValidator(data, 0))
        self.textCtrl2 = wx.TextCtrl(self, validator=DataValidator(data, 0))
    
        checkBtn = wx.Button(self, wx.ID_OK, u"确定")
        checkBtn.Bind(wx.EVT_BUTTON, self.OnClick, checkBtn)
        checkBtn.SetDefault()

        sizer = wx.BoxSizer(wx.VERTICAL)
        gridSizer = wx.FlexGridSizer(2, 2, 5, 5)
        gridSizer.Add(staticText1, 0, wx.ALIGN_LEFT)
        gridSizer.Add(self.textCtrl1, 0, wx.EXPAND)
        gridSizer.Add(staticText2, 0, wx.ALIGN_LEFT)
        gridSizer.Add(self.textCtrl2, 0, wx.EXPAND)
        sizer.Add(gridSizer, 0, wx.EXPAND|wx.ALL, 5)
        sizer.Add(checkBtn, 0, 5)
        self.SetSizer(sizer)
        sizer.Fit(self)
        

    def OnClick(self, event):
        result = float(self.textCtrl1.GetValue()) + float(self.textCtrl2.GetValue())
        wx.MessageBox(str(result),u"结果")
        self.Close()


class MyApp(wx.App):
    def OnInit(self):
        #self.frame = DialogFrame(None)
        #self.frame = PutFrame(None)
        #self.frame = TextFrame(None)
        self.frame = TestFrame(None)
        self.frame.Show()
        #FileDialog()
        return True


app = MyApp()

app.MainLoop()


QQ交流群: 204944806

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值