Tags:
Forums: 

code ด้านล่างนี้ ผมลองดูแล้ว กดปุ่มแล้ว สีไม่ยอมเปลี่ยน
ผมทดสอบดูแล้ว เมื่อกด ปุ่ม มันเข้าไปทำงานใน ToggleRed
แต่สีมันไม่เปลี่ยนครับ

ใครรู้บ้างทำอย่างไร

!/usr/bin/python

togglebuttons.py

import wx

class ToggleButtons(wx.Dialog):
def init(self, parent, id, title):
wx.Dialog.init(self, parent, id, title, size=(300, 200))

    self.colour = wx.Colour(0, 0, 0)

    wx.ToggleButton(self, 1, 'red', (20, 25))
    wx.ToggleButton(self, 2, 'green', (20, 60))
    wx.ToggleButton(self, 3, 'blue', (20, 100))

    self.panel  = wx.Panel(self, -1, (150, 20), (110, 110), style=wx.SUNKEN_BORDER)
    self.panel.SetBackgroundColour(self.colour)

    self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleRed, id=1)
    self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleGreen, id=2)
    self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleBlue, id=3)

    self.Centre()
    self.ShowModal()
    self.Destroy()

def ToggleRed(self, event):
    green = self.colour.Green()
    blue = self.colour.Blue()
    if  self.colour.Red():
        self.colour.Set(0, green, blue)
    else:
        self.colour.Set(255, green, blue)
    self.panel.SetBackgroundColour(self.colour)

def ToggleGreen(self, event):
    red = self.colour.Red()
    blue = self.colour.Blue()
    if  self.colour.Green():
        self.colour.Set(red, 0, blue)
    else:
        self.colour.Set(red, 255, blue)
    self.panel.SetBackgroundColour(self.colour)

def ToggleBlue(self, event):
    red = self.colour.Red()
    green = self.colour.Green()
    if  self.colour.Blue():
        self.colour.Set(red, green, 0)
    else:
        self.colour.Set(red, green, 255)
    self.panel.SetBackgroundColour(self.colour)

app = wx.App()
ToggleButtons(None, -1, 'togglebuttons.py')
app.MainLoop()

Get latest news from Blognone
By: cwt
AndroidRed Hat
on 22 January 2009 - 01:08 #81747

ในเครื่องผมทำงานได้เปลี่ยนสีได้ครับ

  • OSX 10.5.6
  • Python 2.5.1
  • wxPython 2.8.9.1
By: cwt
AndroidRed Hat
on 22 January 2009 - 17:09 #81844

วันนี้ลองเล่นใน linux ก็ทำงานได้ถูกต้องเหมือนกันครับ

By: iknowpython on 23 January 2009 - 08:19 #81976

ผมรันใน Windows XP มันไม่ได้ครับ

By: cwt
AndroidRed Hat
on 23 January 2009 - 15:19 #82060 Reply to:81976

มันบอกว่าอะไรล่ะครับ มี error หรือเปล่า พอดีไม่มี windows ครับ

By: iknowpython on 24 January 2009 - 12:50 #82219

มันไม่มี error ครับ

มันไม่เปลี่ยนสีอะไรเลย ยังเป็นสีเดิมครับ

สงสัย code นี้ เค้าจะไม่ได้ทดสอบใน Windows ละมังครับผม