《python程序設計基礎(第3版)》第9章 GUI課後習題

1.設計一個窗口,並放置一個按鈕,單機按鈕後彈出顏色對話框,關閉顏色對話框後提示選中的顏色。

import wx
class COLOR(wx.Frame):
    def __init__(self, superion):
        wx.Frame.__init__(self, parent=superion, title='COLOR', size=(400,200))
        panel = wx.Panel(self)
        self.buttonOK = wx.Button(parent=panel, label='OK', pos=(70,90))
        self.Bind(wx.EVT_BUTTON, self.OnButtonCheck, self.buttonOK)

    def OnButtonCheck(self, event):
        colorDlg=wx.ColourDialog(None)
        colorDlg.ShowModal()
        color=colorDlg.GetColourData().Colour
        wx.MessageBox(str(color))
  
if __name__ == '__main__':
    app = wx.App()
    frame = COLOR(None)
    frame.Show()
    app.MainLoop()

  

2.設計一個窗體,並放置一個按鈕,按鈕默認文本爲”開始”,單擊按鈕後文本變成”結束”,再次單擊後變爲”開始”,循環切換

import wx
class MyFrame(wx.Frame):
    def __init__(self, superion):
        wx.Frame.__init__(self, parent=superion, title='開始--結束', size=(400,200))
        panel = wx.Panel(self)
        self.buttonOK = wx.Button(parent=panel, label='開始', pos=(70,90))
        self.Bind(wx.EVT_BUTTON, self.OnButtonCheck, self.buttonOK)

    def OnButtonCheck(self, event):
        string=self.buttonOK.GetLabelText()
        if string == '開始':
            self.buttonOK.SetLabelText('結束')
        elif string == '結束':
            self.buttonOK.SetLabelText('開始')
  
if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(None)
    frame.Show()
    app.MainLoop()

 

3.設計一個窗體,模擬QQ登錄界面,當用戶輸入號碼123456和密碼654321時提示正確,否則提示錯誤

 

import wx
class QQ(wx.Frame):
    def __init__(self, superion):
        wx.Frame.__init__(self, parent=superion, title='QQ', size=(250,150),pos=(350,350))
        panel = wx.Panel(self)
        panel.SetBackgroundColour('Red')
        label1=wx.StaticText(panel,-1,'賬號:',pos=(0,10),style=wx.ALIGN_CENTER)
        label1=wx.StaticText(panel,-1,'密碼:',pos=(0,30),style=wx.ALIGN_CENTER)
        self.username=wx.TextCtrl(panel,-1,pos=(70,10),size=(160,20))
        self.password=wx.TextCtrl(panel,pos=(70,30),size=(160,20),style=wx.TE_PASSWORD)
        self.buttonOK = wx.Button(parent=panel, label='登錄', pos=(70,60))
        self.Bind(wx.EVT_BUTTON, self.OnButtonCheck, self.buttonOK)
        self.buttonRes = wx.Button(parent=panel, label='註冊', pos=(70,90))
        self.Bind(wx.EVT_BUTTON, self.OnButtonRes, self.buttonRes)

    def OnButtonCheck(self, event):
        user=self.username.GetValue()
        psw=self.password.GetValue()
        if user == '123456' and psw == '654321':
            wx.MessageBox('成功!')
        else:
            wx.MessageBox('你輸入的賬號或密碼有誤!')
    def OnButtonRes(self,event):
        wx.MessageBox("嘿嘿告訴你個祕密---賬號:123456 密碼:654321")
        
if __name__ == '__main__':
    app = wx.App()
    frame = QQ(None)
    frame.Show()
    app.MainLoop()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章