I'm currently able to get this to do most of what I want to. It draws buttons based on lines from a text file as well as handles the way different button states look. What's really tripping me up right now is when self.input
writes to the text file I have no idea how to get it to redraw everything to add or update buttons based on the new text. I've tried Update
, Refresh
, Show (False)
then Show(True)
and I'm stumped.
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(ch开发者_运维知识库ecked)
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()
Theres no point in calling Update() Refresh()
etc by themselves. They won't auto-magically create your buttons, you need to do that.
For starters I would refactor your __DoButtons()
into two methods, one to create your buttons, and another to get your button data from your file and format it into an appropriate data structure -a list
(and process it accordingly if necessary) which you can then pass to your new `__DoButtons method (which does the actual button creation).
In your onEnter()
you will need to call your __DoButtons()
method and pass in the appropriate data, if you only need to add new buttons why don't you get the data directly from the textEntry
widget and save yourself the hassle of reading it from the file.
精彩评论