Tôi gặp sự cố với wxPython. Một phiên bản đơn giản của mã được đăng bên dưới (không gian trắng, chú thích, v.v ... được xóa để giảm kích thước - nhưng định dạng chung cho chương trình của tôi được giữ nguyên như nhau). Khi tôi chạy tập lệnh, văn bản tĩnh kết thúc chính xác như mong muốn, nhưng các mục khác trong bảng điều khiển không di chuyển xuống (chúng hoạt động như thể statictext chỉ là một dòng và do đó không phải mọi thứ đều hiển thị). Nếu tôi thay đổi kích thước cửa sổ/khung hình theo cách thủ công, thậm chí chỉ là một số lượng nhỏ, mọi thứ sẽ được sửa và hiển thị như mong muốn. Tôi chụp ảnh màn hình để hiển thị hành vi này, nhưng tôi vừa tạo tài khoản này và do đó không có 10 điểm danh tiếng được yêu cầu để được phép đăng ảnh.Các vấn đề về wxPython với Gói StaticText
Tại sao màn hình không hiển thị chính xác để bắt đầu? Tôi đã thử tất cả các loại kết hợp của GetParent(). Refresh() hoặc Update() và GetTopLevelParent(). Update() hoặc Refresh(). Tôi đã thử tất cả mọi thứ tôi có thể nghĩ đến nhưng không thể làm cho nó hiển thị chính xác mà không cần thay đổi kích thước khung/cửa sổ theo cách thủ công. Khi kích thước lại, nó hoạt động chính xác như tôi muốn.
Thông tin:
Windows XP
Python 2.5.2
wxPython 2.8.11.0 (MSW-unicode)
Bất kỳ lời đề nghị? Cảm ơn!
Code:
#! /usr/bin/python
import wx
class StaticWrapText(wx.PyControl):
def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER,
validator=wx.DefaultValidator, name='StaticWrapText'):
wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
self.statictext = wx.StaticText(self, wx.ID_ANY, label, style=style)
self.wraplabel = label
#self.wrap()
def wrap(self):
self.Freeze()
self.statictext.SetLabel(self.wraplabel)
self.statictext.Wrap(self.GetSize().width)
self.Thaw()
def DoGetBestSize(self):
self.wrap()
#print self.statictext.GetSize()
self.SetSize(self.statictext.GetSize())
return self.GetSize()
class TestPanel(wx.Panel):
def __init__(self, *args, **kwargs):
# Init the base class
wx.Panel.__init__(self, *args, **kwargs)
self.createControls()
def createControls(self):
# --- Panel2 -------------------------------------------------------------
self.Panel2 = wx.Panel(self, -1)
msg1 = 'Below is a List of Files to be Processed'
staticBox = wx.StaticBox(self.Panel2, label=msg1)
Panel2_box1_v1 = wx.StaticBoxSizer(staticBox, wx.VERTICAL)
Panel2_box2_h1 = wx.BoxSizer(wx.HORIZONTAL)
Panel2_box3_v1 = wx.BoxSizer(wx.VERTICAL)
self.wxL_Inputs = wx.ListBox(self.Panel2, wx.ID_ANY, style=wx.LB_EXTENDED)
sz = dict(size=(120,-1))
wxB_AddFile = wx.Button(self.Panel2, label='Add File', **sz)
wxB_DeleteFile = wx.Button(self.Panel2, label='Delete Selected', **sz)
wxB_ClearFiles = wx.Button(self.Panel2, label='Clear All', **sz)
Panel2_box3_v1.Add(wxB_AddFile, 0, wx.TOP, 0)
Panel2_box3_v1.Add(wxB_DeleteFile, 0, wx.TOP, 0)
Panel2_box3_v1.Add(wxB_ClearFiles, 0, wx.TOP, 0)
Panel2_box2_h1.Add(self.wxL_Inputs, 1, wx.ALL|wx.EXPAND, 2)
Panel2_box2_h1.Add(Panel2_box3_v1, 0, wx.ALL|wx.EXPAND, 2)
msg = 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
staticMsg = StaticWrapText(self.Panel2, label=msg)
Panel2_box1_v1.Add(staticMsg, 0, wx.ALL|wx.EXPAND, 2)
Panel2_box1_v1.Add(Panel2_box2_h1, 1, wx.ALL|wx.EXPAND, 0)
self.Panel2.SetSizer(Panel2_box1_v1)
# --- Combine Everything -------------------------------------------------
final_vbox = wx.BoxSizer(wx.VERTICAL)
final_vbox.Add(self.Panel2, 1, wx.ALL|wx.EXPAND, 2)
self.SetSizerAndFit(final_vbox)
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
# Init the base class
wx.Frame.__init__(self, *args, **kwargs)
panel = TestPanel(self)
self.SetClientSize(wx.Size(500,500))
self.Center()
class wxFileCleanupApp(wx.App):
def __init__(self, *args, **kwargs):
# Init the base class
wx.App.__init__(self, *args, **kwargs)
def OnInit(self):
# Create the frame, center it, and show it
frame = TestFrame(None, title='Test Frame')
frame.Show()
return True
if __name__ == '__main__':
app = wxFileCleanupApp()
app.MainLoop()
EDIT:
Xem bài của tôi dưới đây cho một giải pháp mà làm việc!
Tôi nên nghĩ về điều đó. Bạn thường muốn gọi Bố cục hoặc trên phụ huynh của các tiện ích hoặc trình chỉnh sửa chứa các tiện ích con. Oh well. Xin lỗi tôi đã không phát hiện ra điều đó. –
Cảm ơn sự giúp đỡ! Nó vẫn không hoàn toàn hoạt động vì nó nên khi tôi phóng to/thu nhỏ cửa sổ. Nhưng hiện tại, nó là đủ tốt. –
Ah, điều này làm cho nó hoạt động ngay cả khi nút tối đa được sử dụng ... trong hàm bọc, sử dụng self.statictext.Wrap (self.GetParent(). GetSize(). Width) thay vì self.statictext.Wrap (self .GetSize(). Chiều rộng) –