我在GNULinux上使用Python 3.8和wxPython 4 (Phoenix)已经好几天了,但进展甚微。我需要一个非常简单的GUI,一个滚动的画布占据了全屏窗口的大部分,而在画布下面有一个滚动的面板,上面有大约4行文字。我想在画布和面板周围留出一些空白。我希望能够自动(而不是绝对)定位。我遇到的问题是:1)滚动条没有出现,2)我使用的是绝对定位,我不太确定我放置的sizer是否能发挥作用。
我一直在破解,尝试各种变化。我已经阅读了我使用的所有调用的API文档。我看了演示和样本。我看了O'Reilly上能找到的每一本书。我仍然觉得自己在理解wxPython的架构方面有很多不清楚的地方。我曾经用Python开发过很多其他的GUI应用,有TkInter和Qt,还有其他语言。所以,我在这里感觉很懵逼。
下面是当前应用的样子。
这是我目前的代码
#!/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()
这是我最近一次修改代码的尝试,以萨克森的Rolf为例。看起来两个txt部分(如果没有注释)没有被写入canvaspanels,但两个部分都在框架的顶部重叠。另外,仍然没有滚动。
#!/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()
我打算把你的画布扔掉,用TextCtrl代替,你可以用任何符合你要求的画布。希望这能给你指明正确的方向,里面有足够的内容来指明方向。
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, 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__(parent, title=title, size=size_value)
#Define widgets
tc1 = wx.TextCtrl(self, size=wx.Size(screen_x-30, 200),
style= wx.TE_MULTILINE|wx.TE_PROCESS_ENTER|wx.TE_DONTWRAP|wx.TE_AUTO_URL)
tc1.SetBackgroundColour('#cc2000')
# Some Control buttons
self.b1 = wx.Button(self, -1, "Button 1")
self.b2 = wx.Button(self, -1, "Button 2")
self.b3 = wx.Button(self, -1, "Quit")
tc2 = wx.TextCtrl(self, size=wx.Size(screen_x-30, 1000),
style= wx.TE_MULTILINE|wx.TE_PROCESS_ENTER|wx.TE_DONTWRAP)
tc2.SetBackgroundColour('#d0d020')
#Bind events to functions
self.b3.Bind(wx.EVT_BUTTON, self.OnQuit)
#Load initial data
try:
with open('tips.txt') as fp:
txt_value2 = fp.read()
except:
txt_value2 = "tips.txt file not found"
txt_value2 = txt_value2 * 20
tc1.AppendText("No data yet\n")
tc1.AppendText("www.nodatayet.com\n")
tc1.AppendText(" "*20)
tc1.AppendText("X\n")
tc2.write(txt_value2)
#Define sizers
sizer = wx.BoxSizer(wx.VERTICAL)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
#Populate sizers
button_sizer.Add(self.b1)
button_sizer.Add(self.b2)
button_sizer.Add(self.b3)
sizer.Add(tc1, proportion=0, flag=wx.ALL, border=20)
sizer.Add(button_sizer, proportion=0, flag=wx.LEFT, border=40)
sizer.Add(tc2, proportion=1, flag=wx.ALL, border=40)
self.SetSizer(sizer)
self.Show()
def OnQuit(self, event):
self.Destroy()
app = wx.App()
MainWindow(None,'Git Branch History')
app.MainLoop()