【发布时间】:2020-06-02 19:47:48
【问题描述】:
我已经在 GNU/Linux 上研究 Python 3.8 和 wxPython 4 (Phoenix) 好几天了,但进展甚微。我需要一个非常简单的 GUI,一个滚动的画布占据了全屏窗口的大部分,一个滚动的面板在其下方保存了大约 4 行文本。我想要画布和面板周围的一些边距。我希望能够自动(而不是绝对)定位。我遇到的问题是 1) 滚动条没有出现,2) 我使用的是绝对定位,我不确定我放置的尺寸器是否正常工作。
我一直在四处寻找,尝试各种变化。我已经阅读了我使用的所有调用的 API 文档。我看过演示和示例。我看过我能在 O'Reilly 上找到的每一本书。我仍然觉得我对理解 wxPython 的架构还有些摸不着头脑。我在 Python 中使用 TkInter 和 Qt 以及其他语言开发了许多其他 GUI 应用程序。所以,我感觉这里很密集。
这是当前应用的外观:
这是我当前的代码:
#!/usr/bin/env python3.8
import wx
class MainWindow(wx.Frame):
def __init__(self, title):
screen_x = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
screen_y = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
size_value = wx.Size(screen_x, screen_y)
wx.Frame.__init__(self, None, title=title, size=size_value)
# why does this not work?
# super(MainWindow, self).__init__(self, None, title=title,
# size=size_value)
canvas1 = wx.ScrolledCanvas(self, size=wx.Size(screen_x-30, 300),
pos=wx.Point(15, 15))
canvas1.SetBackgroundColour('#cc20cc')
canvas1.AlwaysShowScrollbars(True, True)
canvas1.SetAutoLayout(1)
canvas1.SetScrollbar(wx.VERTICAL, 0, 10, 100)
canvas1.SetScrollbar(wx.HORIZONTAL, 0, 10, 100)
canvas1.SetScrollRate(1, 1)
# with open('/home/kbuchs/.emacs') as fp:
# txt_value = fp.read()
# txt = wx.StaticText(canvas1, label=txt_value)
canvas2 = wx.ScrolledCanvas(self, size=wx.Size(screen_x-30, 1000),
pos=wx.Point(15, 315))
canvas2.SetBackgroundColour('#d0d020')
canvas2.AlwaysShowScrollbars(True, True)
canvas2.SetAutoLayout(1)
canvas2.SetScrollRate(1, 1)
with open('/home/kbuchs/.bashrc') as fp:
txt_value2 = fp.read()
txt2 = wx.StaticText(canvas2, label=txt_value2)
sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer = sizer
sizer.Add(canvas1, wx.ID_ANY, wx.ALL, 20)
sizer.Add(canvas2, wx.ID_ANY, wx.ALL, 40)
self.Show()
app = wx.App()
MainWindow('Git Branch History')
app.MainLoop()
更新 6 月 3 日晚上 10:30 CDT。 这是我在 Saxony 的 Rolf 示例之后对代码的最新修订尝试。似乎两个 txt 部分(如果未注释)没有被写入画布/面板,但两者都在框架的顶部重叠。此外,仍然没有滚动。
#!/usr/bin/env python3.8
import wx
import wx.lib.scrolledpanel as sp
class MainWindow(wx.Frame):
def __init__(self, title):
screen_x = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
screen_y = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
size_value = wx.Size(screen_x, screen_y)
super(MainWindow, self).__init__(None, title=title, size=size_value)
# canvas1 = wx.ScrolledCanvas(self, id=-1,
canvas1 = sp.ScrolledPanel(self, id=-1,
size=wx.Size(screen_x, screen_y-200))
# pos=wx.Point(15, 15))
canvas1.SetBackgroundColour('#ccffff')
canvas1.AlwaysShowScrollbars(True, True)
canvas1.SetAutoLayout(1)
canvas1.SetupScrolling()
with open('/home/kbuchs/.emacs') as fp:
txt_value1 = fp.read()
long_line = 900*'-' + '\n'
# txt1 = wx.StaticText(canvas1, label=long_line+txt_value1)
# txt1.SetBackgroundColour('#eeffff')
# txt1Sizer = wx.BoxSizer(wx.VERTICAL)
# txt1Sizer.Add(txt1, proportion=0, border=5)
canvas1Sizer = wx.BoxSizer(wx.VERTICAL)
canvas1Sizer.Add(canvas1, proportion=0, flag=wx.CENTER|wx.ALL|wx.EXPAND, border=5)
# canvas1Sizer.Add(txt1Sizer, proportion=0)
# canvas1Sizer.Add(txt1, proportion=0, flag=wx.ALL, border=5)
# canvas2 = wx.ScrolledCanvas(self, id=-1,
canvas2 = sp.ScrolledPanel(self, id=-1,
size=wx.Size(screen_x, 200))
# pos=wx.Point(15, 315))
canvas2.SetBackgroundColour('#ffffcc')
canvas2.AlwaysShowScrollbars(True, True)
canvas2.SetAutoLayout(1)
canvas2.SetupScrolling()
with open('/home/kbuchs/.bashrc') as fp:
txt_value2 = fp.read()
# txt2 = wx.StaticText(canvas2, label=long_line+txt_value2)
# txt2.SetBackgroundColour('#ffffee')
canvas2Sizer = wx.BoxSizer(wx.VERTICAL)
canvas2Sizer.Add(canvas2, proportion=0, flag=wx.CENTER|wx.ALL|wx.EXPAND, border=5)
# canvas2Sizer.Add(txt2, proportion=0, flag=wx.CENTER|wx.ALL, border=20)
sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer = sizer
sizer.Add(canvas1Sizer, proportion=0, flag=wx.CENTER|wx.ALL|wx.EXPAND, border=5)
sizer.Add(canvas2Sizer, proportion=0, flag=wx.CENTER|wx.ALL|wx.EXPAND, border=5)
self.Show()
app = wx.App()
MainWindow('Git Branch History')
app.MainLoop()
【问题讨论】:
-
我现在没有时间,但您需要在演出前分配尺寸器,即
self.SetSizer(sizer)。使用尺寸测量仪直接定位可能会产生奇怪的外观。 Sizer 一开始会让你发疯;) -
嗯?尺寸更大的东西都是在演出之前。
-
是的,但你忽略了
SetSizer,这是我的意思!
标签: wxpython