我遇到了麻烦,我相信是sizer。我不能按照我想要的方式设置笔记本的大小。现在我放了一个按钮,按钮的位置决定了笔记本的大小(它会拉伸它)但是我希望它能够在不使用按钮的情况下完成。我玩过它但是我不能让它自己改变。
如果您不明白我的意思,请运行以下代码。
import random
import wx
[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1LISTBOX1,
] = [wx.NewId() for _init_ctrls in range(4)]
########################################################################
class TabPanel(wx.Panel):
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent)
colors = ["red", "blue", "gray", "yellow", "green"]
self.SetBackgroundColour(random.choice(colors))
sampleList = ['0', '1', '2', '3', '4']
listBox = wx.ListBox(self, -1, (20, 20), (80, 120), sampleList, wx.LB_SINGLE)
listBox.SetSelection(3)
#This is the button!!!!
btn = wx.Button(self, label="Create new")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(btn,300,400, wx.ALL, 10)
self.SetSizer(sizer)
########################################################################
class DemoFrame(wx.Frame):
"""
Frame that holds all other widgets
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,
"Notebook Tutorial",
size=(600,400)
)
panel = wx.Panel(self)
notebook = wx.Notebook(panel)
tabOne = TabPanel(notebook)
notebook.AddPage(tabOne, "things")
tabTwo = TabPanel(notebook)
notebook.AddPage(tabTwo, "other things")
sizer = wx.BoxSizer(400)
sizer.Add(notebook)
panel.SetSizer(sizer)
#menu bar
status=self.CreateStatusBar()
menubar=wx.MenuBar()
first=wx.Menu()
second=wx.Menu()
first.Append(wx.NewId(),"New","Creates A new file")
first.Append(wx.NewId(),"ADID","Yo")
menubar.Append(first,"File")
menubar.Append(second,"Edit")
self.SetMenuBar(menubar)
self.Layout()
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = DemoFrame()
app.MainLoop()
非常感谢你寻找。
答案 0 :(得分:0)
尝试这样的事情:
COLORS = ["red", "blue", "black", "yellow", "green"]
NUMBERS = ['0', '1', '2', '3', '4']
PANELS = ["Things", "More things", "Ultra!"]
import random
import wx
class TabPanel(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
self.SetBackgroundColour(random.choice(COLORS))
self.listBox = wx.ListBox(self, size=(200, -1), choices=NUMBERS, style=wx.LB_SINGLE)
# self.button = wx.Button(self, label="Something else here? Maybe!")
self.sizer = wx.BoxSizer()
self.sizer.Add(self.listBox, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
# self.sizer.Add(self.button, proportion=1, flag=wx.ALL)
self.SetSizer(self.sizer)
class MyNotebook(wx.Notebook):
def __init__(self, *args, **kwargs):
wx.Notebook.__init__(self, *args, **kwargs)
self.panels = []
for name in PANELS:
panel = TabPanel(self)
self.panels.append(panel)
self.AddPage(panel, name)
class MyPanel(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
self.notebook = MyNotebook(self, size=(400, -1))
# self.button = wx.Button(self, label="Something else here? Maybe!")
self.sizer = wx.BoxSizer()
self.sizer.Add(self.notebook, proportion=0, flag=wx.EXPAND)
# self.sizer.Add(self.button, proportion=0)
self.SetSizer(self.sizer)
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = MyPanel(self)
self.status = self.CreateStatusBar()
self.menubar = wx.MenuBar()
first=wx.Menu()
second=wx.Menu()
first.Append(wx.NewId(), "New", "Creates A new file")
first.Append(wx.NewId(), "ADID", "Yo")
self.menubar.Append(first, "File")
self.menubar.Append(second, "Edit")
self.SetMenuBar(self.menubar)
self.Show()
app = wx.App(False)
win = MainWindow(None, size=(600, 400))
app.MainLoop()
取消注释按钮,了解如何添加更多内容。您可以在我的代码中找到一些有用的wxPython编程提示。我懒得描述一切; - )。