并排的两个面板(一个带有TextCtrl)-wxPython
这是我的代码:并排的两个面板(一个带有TextCtrl)-wxPython,wxpython,panel,Wxpython,Panel,这是我的代码: import wx #For graphics' interface import os #For operating system compatibility class MainWindow(wx.Frame): def __init__(self, parent, id, title): #SETUP wx.Frame.__init__(self, parent, wx.ID_ANY, title = "MyTitle", si
import wx #For graphics' interface
import os #For operating system compatibility
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
#SETUP
wx.Frame.__init__(self, parent, wx.ID_ANY, title = "MyTitle", size = (550,300))
self.dirname=''
#CREATE
self.create_status_bar()
self.create_menu_bar()
self.create_text_panel()
self.create_graphics_panel()
self.SetAutoLayout(True)
lc = wx.LayoutConstraints()
lc.top.SameAs(self, wx.Top, 5)
lc.left.SameAs(self, wx.Left, 5)
lc.bottom.SameAs(self, wx.Bottom, 5)
lc.right.PercentOf(self, wx.Right, 40)
self.panelA.SetConstraints(lc)
lc = wx.LayoutConstraints()
lc.top.SameAs(self, wx.Top, 5)
lc.left.RightOf(self.panelA, 5)
lc.bottom.SameAs(self, wx.Bottom, 5)
lc.right.SameAs(self, wx.Right, 5)
self.panelB.SetConstraints(lc)
# FUNCTIONS
# ------------------------------------------------------------------------------
def create_status_bar(self):
self.CreateStatusBar() #A Statusbar at the bottom of the window
def create_menu_bar(self):
# File Menu
filemenu= wx.Menu()
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open file to edit")
filemenu.AppendSeparator()
menuSave = filemenu.Append(wx.ID_SAVE, "&Save", "Save the file")
menuSaveAs = filemenu.Append(wx.ID_SAVEAS, "Save &As", "Save the file with a new name")
filemenu.AppendSeparator()
menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate communication and close window")
#The Menu Bar
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") #Adding the "File" menu to the 'menuBar'
self.SetMenuBar(menuBar) #Adding the 'menuBar' to the Frame content
#Event binding
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
self.Bind(wx.EVT_MENU, self.OnSaveAs, menuSaveAs)
def create_text_panel(self):
self.panelA = wx.Panel(self)
self.panelA.control = wx.TextCtrl(self.panelA, wx.ID_ANY, style = wx.TE_MULTILINE) #Text area with multiline
self.panelA.SetBackgroundColour(wx.WHITE)
def create_graphics_panel(self):
self.panelB = wx.Panel(self)
self.panelB.SetBackgroundColour(wx.BLACK)
# EVENTS
# ------------------------------------------------------------------------------
def OnExit(self, event):
self.Close(True) #Close the frame
def OnOpen(self, event):
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.panelA.control.SetValue(f.read())
f.close()
dlg.Destroy()
def OnSave(self, event):
f = open(os.path.join(self.dirname, self.filename), 'w')
f.write(self.panelA.control.GetValue())
f.close()
def OnSaveAs(self, event):
file_choices = "TXT (*.txt)|*.txt"
dlg = wx.FileDialog(self, message = "Save file as...", defaultDir = os.getcwd(), defaultFile = self.filename, wildcard = file_choices, style = wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
f = open(os.path.join(dlg.GetDirectory(), dlg.GetFilename()), 'w')
f.write(self.panelA.control.GetValue())
f.close()
# RUN!
# ------------------------------------------------------------------------------
if __name__ == '__main__':
app = wx.PySimpleApp(False)
app.frame = MainWindow(None, wx.ID_ANY, "tSock - Adaptation Technologies")
app.frame.Show()
app.MainLoop()
我的问题是,我似乎无法让它按我想要的方式工作。我想有两个面板:左边的一个用作文本编辑器,右边的另一个用于接收来自串行端口的输入(以后的工作)
然而,我似乎无法让左侧面板正常工作。是否有我遗漏的内容?您需要向panelA中添加一个大小调整器,并将文本控件添加到大小调整器中,以便它可以适当扩展并占用空间:
import wx #For graphics' interface
import os #For operating system compatibility
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
#SETUP
wx.Frame.__init__(self, parent, wx.ID_ANY, title = "MyTitle", size = (550,300))
self.dirname=''
#CREATE
self.create_status_bar()
self.create_menu_bar()
self.create_text_panel()
self.create_graphics_panel()
self.SetAutoLayout(True)
lc = wx.LayoutConstraints()
lc.top.SameAs(self, wx.Top, 5)
lc.left.SameAs(self, wx.Left, 5)
lc.bottom.SameAs(self, wx.Bottom, 5)
lc.right.PercentOf(self, wx.Right, 40)
self.panelA.SetConstraints(lc)
lc = wx.LayoutConstraints()
lc.top.SameAs(self, wx.Top, 5)
lc.left.RightOf(self.panelA, 5)
lc.bottom.SameAs(self, wx.Bottom, 5)
lc.right.SameAs(self, wx.Right, 5)
self.panelB.SetConstraints(lc)
# FUNCTIONS
# ------------------------------------------------------------------------------
def create_status_bar(self):
self.CreateStatusBar() #A Statusbar at the bottom of the window
def create_menu_bar(self):
# File Menu
filemenu= wx.Menu()
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open file to edit")
filemenu.AppendSeparator()
menuSave = filemenu.Append(wx.ID_SAVE, "&Save", "Save the file")
menuSaveAs = filemenu.Append(wx.ID_SAVEAS, "Save &As", "Save the file with a new name")
filemenu.AppendSeparator()
menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate communication and close window")
#The Menu Bar
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") #Adding the "File" menu to the 'menuBar'
self.SetMenuBar(menuBar) #Adding the 'menuBar' to the Frame content
#Event binding
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
self.Bind(wx.EVT_MENU, self.OnSaveAs, menuSaveAs)
def create_text_panel(self):
self.panelA = wx.Panel(self)
self.panelA.control = wx.TextCtrl(self.panelA, wx.ID_ANY, style = wx.TE_MULTILINE) #Text area with multiline
self.panelA.SetBackgroundColour(wx.WHITE)
panelA_sizer = wx.BoxSizer(wx.VERTICAL)
panelA_sizer.Add(self.panelA.control, 1, wx.EXPAND)
self.panelA.SetSizer(panelA_sizer)
def create_graphics_panel(self):
self.panelB = wx.Panel(self)
self.panelB.SetBackgroundColour(wx.BLACK)
# EVENTS
# ------------------------------------------------------------------------------
def OnExit(self, event):
self.Close(True) #Close the frame
def OnOpen(self, event):
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.panelA.control.SetValue(f.read())
f.close()
dlg.Destroy()
def OnSave(self, event):
f = open(os.path.join(self.dirname, self.filename), 'w')
f.write(self.panelA.control.GetValue())
f.close()
def OnSaveAs(self, event):
file_choices = "TXT (*.txt)|*.txt"
dlg = wx.FileDialog(self, message = "Save file as...", defaultDir = os.getcwd(), defaultFile = self.filename, wildcard = file_choices, style = wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
f = open(os.path.join(dlg.GetDirectory(), dlg.GetFilename()), 'w')
f.write(self.panelA.control.GetValue())
f.close()
# RUN!
# ------------------------------------------------------------------------------
if __name__ == '__main__':
app = wx.PySimpleApp(False)
app.frame = MainWindow(None, wx.ID_ANY, "tSock - Adaptation Technologies")
app.frame.Show()
app.MainLoop()
还要注意,wx.PySimpleApp
已被弃用。您现在应该使用wx.App(False)
。我还建议创建一个顶级面板,其他两个面板继承自该面板,或者使用拆分器小部件
以下是如何使用顶级面板:
import wx #For graphics' interface
import os #For operating system compatibility
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
#SETUP
wx.Frame.__init__(self, parent, wx.ID_ANY, title = "MyTitle", size = (550,300))
self.dirname=''
self.top_panel = wx.Panel(self)
self.main_sizer = wx.BoxSizer(wx.HORIZONTAL)
#CREATE
self.create_status_bar()
self.create_menu_bar()
self.create_text_panel()
self.create_graphics_panel()
self.SetAutoLayout(True)
lc = wx.LayoutConstraints()
lc.top.SameAs(self, wx.Top, 5)
lc.left.SameAs(self, wx.Left, 5)
lc.bottom.SameAs(self, wx.Bottom, 5)
lc.right.PercentOf(self, wx.Right, 40)
self.panelA.SetConstraints(lc)
lc = wx.LayoutConstraints()
lc.top.SameAs(self, wx.Top, 5)
lc.left.RightOf(self.panelA, 5)
lc.bottom.SameAs(self, wx.Bottom, 5)
lc.right.SameAs(self, wx.Right, 5)
self.panelB.SetConstraints(lc)
self.top_panel.SetSizer(self.main_sizer)
# FUNCTIONS
# ------------------------------------------------------------------------------
def create_status_bar(self):
self.CreateStatusBar() #A Statusbar at the bottom of the window
def create_menu_bar(self):
# File Menu
filemenu= wx.Menu()
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open file to edit")
filemenu.AppendSeparator()
menuSave = filemenu.Append(wx.ID_SAVE, "&Save", "Save the file")
menuSaveAs = filemenu.Append(wx.ID_SAVEAS, "Save &As", "Save the file with a new name")
filemenu.AppendSeparator()
menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate communication and close window")
#The Menu Bar
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") #Adding the "File" menu to the 'menuBar'
self.SetMenuBar(menuBar) #Adding the 'menuBar' to the Frame content
#Event binding
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
self.Bind(wx.EVT_MENU, self.OnSaveAs, menuSaveAs)
def create_text_panel(self):
self.panelA = wx.Panel(self.top_panel)
self.panelA.control = wx.TextCtrl(self.panelA, wx.ID_ANY, style = wx.TE_MULTILINE) #Text area with multiline
self.panelA.SetBackgroundColour(wx.WHITE)
panelA_sizer = wx.BoxSizer(wx.VERTICAL)
panelA_sizer.Add(self.panelA.control, 1, wx.EXPAND)
self.panelA.SetSizer(panelA_sizer)
self.main_sizer.Add(self.panelA, 1, wx.EXPAND)
def create_graphics_panel(self):
self.panelB = wx.Panel(self.top_panel)
self.panelB.SetBackgroundColour(wx.BLACK)
self.main_sizer.Add(self.panelB, 1, wx.EXPAND)
# EVENTS
# ------------------------------------------------------------------------------
def OnExit(self, event):
self.Close(True) #Close the frame
def OnOpen(self, event):
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.panelA.control.SetValue(f.read())
f.close()
dlg.Destroy()
def OnSave(self, event):
f = open(os.path.join(self.dirname, self.filename), 'w')
f.write(self.panelA.control.GetValue())
f.close()
def OnSaveAs(self, event):
file_choices = "TXT (*.txt)|*.txt"
dlg = wx.FileDialog(self, message = "Save file as...", defaultDir = os.getcwd(), defaultFile = self.filename, wildcard = file_choices, style = wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
f = open(os.path.join(dlg.GetDirectory(), dlg.GetFilename()), 'w')
f.write(self.panelA.control.GetValue())
f.close()
# RUN!
# ------------------------------------------------------------------------------
if __name__ == '__main__':
app = wx.PySimpleApp(False)
app.frame = MainWindow(None, wx.ID_ANY, "tSock - Adaptation Technologies")
app.frame.Show()
app.MainLoop()
我最喜欢的另一种方法是将每个面板放入自己的类中。我相信这会给你更多的灵活性。有一种方法可以做到这一点:
import wx #For graphics' interface
import os #For operating system compatibility
class TextPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.SetBackgroundColour(wx.WHITE)
self.control = wx.TextCtrl(self, style = wx.TE_MULTILINE) #Text area with multiline
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.control, 1, wx.EXPAND)
self.SetSizer(sizer)
class GraphicsPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.SetBackgroundColour(wx.BLACK)
class TopPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
main_sizer = wx.BoxSizer(wx.HORIZONTAL)
text = TextPanel(self)
main_sizer.Add(text, 1, wx.EXPAND)
graphics = GraphicsPanel(self)
main_sizer.Add(graphics, 1, wx.EXPAND)
self.SetSizer(main_sizer)
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
#SETUP
wx.Frame.__init__(self, parent, wx.ID_ANY, title = "MyTitle", size = (550,300))
self.dirname=''
self.top_panel = TopPanel(self)
#CREATE
self.create_status_bar()
self.create_menu_bar()
# FUNCTIONS
# ------------------------------------------------------------------------------
def create_status_bar(self):
self.CreateStatusBar() #A Statusbar at the bottom of the window
def create_menu_bar(self):
# File Menu
filemenu= wx.Menu()
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open file to edit")
filemenu.AppendSeparator()
menuSave = filemenu.Append(wx.ID_SAVE, "&Save", "Save the file")
menuSaveAs = filemenu.Append(wx.ID_SAVEAS, "Save &As", "Save the file with a new name")
filemenu.AppendSeparator()
menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate communication and close window")
#The Menu Bar
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") #Adding the "File" menu to the 'menuBar'
self.SetMenuBar(menuBar) #Adding the 'menuBar' to the Frame content
#Event binding
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
self.Bind(wx.EVT_MENU, self.OnSaveAs, menuSaveAs)
# EVENTS
# ------------------------------------------------------------------------------
def OnExit(self, event):
self.Close(True) #Close the frame
def OnOpen(self, event):
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.panelA.control.SetValue(f.read())
f.close()
dlg.Destroy()
def OnSave(self, event):
f = open(os.path.join(self.dirname, self.filename), 'w')
f.write(self.panelA.control.GetValue())
f.close()
def OnSaveAs(self, event):
file_choices = "TXT (*.txt)|*.txt"
dlg = wx.FileDialog(self, message = "Save file as...", defaultDir = os.getcwd(), defaultFile = self.filename, wildcard = file_choices, style = wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
f = open(os.path.join(dlg.GetDirectory(), dlg.GetFilename()), 'w')
f.write(self.panelA.control.GetValue())
f.close()
# RUN!
# ------------------------------------------------------------------------------
if __name__ == '__main__':
app = wx.PySimpleApp(False)
app.frame = MainWindow(None, wx.ID_ANY, "tSock - Adaptation Technologies")
app.frame.Show()
app.MainLoop()
请容忍我作为一个noob:我远不是一个程序员,我不知道你想要左面板是什么样子,你使用的布局约束我从来没有使用过,我使用的是尺寸标注器,但我确实看到你没有为TextCtrl设置约束,这可能是你所缺少的。我愿意接受所有的可能性。如果你觉得sizers应该是一种选择,请随意写一个答案:)@Yoriz我之所以使用它们,是因为我在一个例子中发现了它们,而不是因为我是它们的铁杆爱好者。请教我师父!非常感谢你!如何创建顶层面板?我应该去上新课吗?我该怎么做呢?我又添加了几个可能对您有所帮助的示例。是的!!我在考虑如何做第三种方法,因为这是我在第二次会议上提出的,但我不知道如何做。非常感谢你,我学到了很多东西!:)只是一个后续问题:如何调用
OnOpen
、OnSave
和onsavas
事件?我应该使用self.top\u panel.panelA.control
?我建议使用pubsub: