0
我目前能夠得到這個去做大部分我想要的。它根據文本文件中的行繪製按鈕,並處理不同按鈕狀態的外觀。現在真正絆倒我的是當self.input
寫入文本文件時,我不知道如何讓它重新繪製所有內容以基於新文本添加或更新按鈕。我試過Update
,Refresh
,Show (False)
然後Show(True)
,我很難過。使用DC即時繪製wx.BitMapButtons?
import wx
import mmap
import re
class pt:
with open('note.txt', "r+") as Note:
buf = mmap.mmap(Note.fileno(), 0)
TL = 0
readline = buf.readline
while readline():
TL += 1
Note.closed
class MainWindow(wx.Frame):
def __init__(self, parent, title):
w, h = wx.GetDisplaySize()
x = w * 0
y = h - bdepth
wx.Frame.__init__(self, parent, title = title, pos = (x, y), size = (200,bdepth), style = wx.STAY_ON_TOP)
self.input = wx.TextCtrl(self, -1, "", (6, pt.TL * 64 + 4), (184, 24))
self.Bind(wx.EVT_TEXT_ENTER, self.OnEnter, self.input)
self.__DoButtons()
self.Show(True)
def __DoButtons(self):
Note = open('note.txt', "r+")
for i, line in enumerate(Note):
strip = line.rstrip()
todo = strip.lstrip('!')
self.check = re.match('!', strip)
self.priority = re.search('(\!$)', strip)
checkmark = wx.Image('check.bmp', wx.BITMAP_TYPE_BMP)
bullet = wx.Image('bullet.bmp', wx.BITMAP_TYPE_BMP)
exclaim = wx.Image('exclaim.bmp', wx.BITMAP_TYPE_BMP)
solid = wx.EmptyBitmap(200,64,-1)
dc = wx.MemoryDC()
dc.SelectObject(solid)
solidpen = wx.Pen(wx.Colour(75,75,75),wx.SOLID)
dc.SetPen(solidpen)
dc.DrawRectangle(0, 0, 200, 64)
dc.SetTextForeground(wx.Colour(255, 255, 255))
dc.DrawBitmap(wx.BitmapFromImage(bullet, 32), 10, 28)
dc.DrawText(todo, 30, 24)
dc.SelectObject(wx.NullBitmap)
checked = wx.EmptyBitmap(200,64,-1)
dc = wx.MemoryDC()
dc.SelectObject(checked)
checkedpen = wx.Pen(wx.Colour(50,50,50),wx.SOLID)
dc.SetPen(checkedpen)
dc.DrawRectangle(0, 0, 200, 50)
dc.SetTextForeground(wx.Colour(200, 255, 0))
dc.DrawBitmap(wx.BitmapFromImage(checkmark, 32), 6, 24)
dc.DrawText(todo, 30, 24)
dc.SelectObject(wx.NullBitmap)
hover = wx.EmptyBitmap(200,64,-1)
dc = wx.MemoryDC()
dc.SelectObject(hover)
hoverpen = wx.Pen(wx.Colour(100,100,100),wx.SOLID)
dc.SetPen(hoverpen)
dc.DrawRectangle(0, 0, 200, 64)
dc.SetTextForeground(wx.Colour(255, 255, 255))
dc.DrawBitmap(wx.BitmapFromImage(bullet, 32), 10, 28)
dc.DrawText(todo, 30, 24)
dc.SelectObject(wx.NullBitmap)
important = wx.EmptyBitmap(200,64,-1)
dc = wx.MemoryDC()
dc.SelectObject(important)
importantpen = wx.Pen(wx.Colour(75,75,75),wx.SOLID)
dc.SetPen(importantpen)
dc.DrawRectangle(0, 0, 200, 50)
dc.SetTextForeground(wx.Colour(255, 180, 0))
dc.DrawBitmap(wx.BitmapFromImage(exclaim, 32), 6, 24)
dc.DrawText(todo, 30, 24)
dc.SelectObject(wx.NullBitmap)
importanthover = wx.EmptyBitmap(200,64,-1)
dc = wx.MemoryDC()
dc.SelectObject(importanthover)
importanthoverpen = wx.Pen(wx.Colour(100,100,100),wx.SOLID)
dc.SetPen(importanthoverpen)
dc.DrawRectangle(0, 0, 200, 50)
dc.SetTextForeground(wx.Colour(255, 180, 0))
dc.DrawBitmap(wx.BitmapFromImage(exclaim, 32), 6, 24)
dc.DrawText(todo, 30, 24)
dc.SelectObject(wx.NullBitmap)
if self.check is None and self.priority is None:
bmp = solid
elif self.priority is None:
bmp = checked
else:
bmp = important
b = wx.BitmapButton(self, i + 800, bmp, (0, i * 64), (solid.GetWidth(), solid.GetHeight()), style = wx.NO_BORDER)
b.SetBitmapDisabled(checked)
if self.check is None and self.priority is None:
b.SetBitmapHover(hover)
elif self.priority is None:
b.SetBitmapHover(checked)
else:
b.SetBitmapHover(importanthover)
Note.closed
def OnClick(self, event):
button = event.GetEventObject()
button.None
print('cheese')
def OnEnter(self, event):
editnote = open('note.txt', 'r+')
editnote.write(self.input.GetValue())
editnote.close()
self.Update()
bdepth = pt.TL * 64 + 32
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
那麼這很好。我看到其他人建議以這種方式使用它,但我可能錯過了他們的背景,因爲起初Binds對我來說看起來很神奇。 好的,我將從擴展'class pt'開始,實際創建一個'list'。可能會更加高效並且有用。 我會詳細閱讀,一旦我取得更多進展,請回到此主題。 謝謝。 – Tryst 2010-09-17 10:18:16
這非常有效,它引發了一些新問題,但只是因爲我的部分網站。再次感謝! – Tryst 2010-09-18 00:32:14