wx Python适合控件的屏幕

时间:2017-10-31 09:22:12

标签: wxpython

这让我很生气。有一个简单的屏幕,但它不适合并显示所有控件。我做错了什么?

[编辑]我在Ubuntu 16.4上运行它,Python 3.5.3,wx.Python 4.0.0b2 gtk2(凤凰)[\编辑]

import wx

class MainEdit(wx.Frame):
    def __init__(self, parent=None, title="Main Edit"):
        wx.Frame.__init__(self, parent, -1, title=title)
        panel = MainPanel(self)
        self.Fit()


class MainPanel(wx.Panel):
    def __init__(self, frame):
        wx.Panel.__init__(self, frame)
        txt_size = (300, 150)
        txt_style = wx.VSCROLL|wx.HSCROLL|wx.BORDER_SIMPLE|wx.TE_MULTILINE
        self.txt_A = wx.TextCtrl(self, size=txt_size, style=txt_style)
        self.txt_B = wx.TextCtrl(self, size=txt_size, style=txt_style)
        cmd_refresh = wx.Button(self, wx.ID_REFRESH)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.txt_A, flag=wx.ALL, border=10)
        main_sizer.Add(self.txt_B, flag=wx.ALL, border=10)
        main_sizer.Add(cmd_refresh, flag=wx.ALL, border=10)
        self.SetSizerAndFit(main_sizer)

if __name__ == '__main__':
    app = wx.App()
    frame = MainEdit()
    frame.Show()
    app.MainLoop()

[编辑2017 11 02显示图片]

这是我得到的效果

enter image description here

[\ EDIT]

[编辑2017 11 03 11:20]这是我将代码更改为Rolf建议时的输出

11:18:29: Debug: ClientToScreen cannot work when toplevel window is not shown
1 (0, 0, 20, 20)
11:18:29: Debug: ClientToScreen cannot work when toplevel window is not shown
2 (0, 0, 320, 389)
3 (0, 0, 400, 250)

屏幕仍然被截断

[/编辑]

[\编辑2017 11 04 09:20] 以下代码有效:

import wx

class MainEdit(wx.Frame):
    def __init__(self, parent=None, title="Main Edit"):
        wx.Frame.__init__(self, parent, -1, title=title)
        txt_size = (300, 150)
        txt_style = wx.VSCROLL|wx.HSCROLL|wx.BORDER_SIMPLE|wx.TE_MULTILINE
        self.txt_A = wx.TextCtrl(self, size=txt_size, style=txt_style)
        self.txt_B = wx.TextCtrl(self, size=txt_size, style=txt_style)
        cmd_refresh = wx.Button(self, wx.ID_REFRESH)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.txt_A, flag=wx.ALL, border=10)
        main_sizer.Add(self.txt_B, flag=wx.ALL, border=10)
        main_sizer.Add(cmd_refresh, flag=wx.ALL, border=10)
        self.SetSizerAndFit(main_sizer)


if __name__ == '__main__':
    app = wx.App()
    frame = MainEdit()
    frame.Show()
    app.MainLoop()

如您所见,我已删除了面板。但是,我被告知here“框架应该只有一个孩子,一个小组。”

那么构建wxPython程序的推荐方法是什么?[/ EDIT]

3 个答案:

答案 0 :(得分:1)

此代码修复了它。我在MainFrame中添加了一个sizer并将面板放入其中。它仍然没有回答为什么原始代码在某些PC上运行而不是我的

import wx

class MainEdit(wx.Frame):
    def __init__(self, parent=None, title="Main Edit"):
        wx.Frame.__init__(self, parent, -1, title=title)
        panel = MainPanel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel)
        self.SetSizerAndFit(sizer)

class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        txt_size = (300, 150)
        txt_style = wx.VSCROLL|wx.HSCROLL|wx.BORDER_SIMPLE|wx.TE_MULTILINE
        self.txt_A = wx.TextCtrl(self, size=txt_size, style=txt_style)
        self.txt_B = wx.TextCtrl(self, size=txt_size, style=txt_style)
        cmd_refresh = wx.Button(self, wx.ID_REFRESH)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.txt_A, flag=wx.ALL, border=10)
        main_sizer.Add(self.txt_B, flag=wx.ALL, border=10)
        main_sizer.Add(cmd_refresh, flag=wx.ALL, border=10)
        self.SetSizer(main_sizer)


if __name__ == '__main__':
    app = wx.App()
    frame = MainEdit()
    frame.Show()
    app.MainLoop()

请注意,必须在框架级别而不是面板上完成调整。事实上,如果我在这里用SetSizer替换SetSizerAndFit,我会遇到与我开始时相同的问题。

答案 1 :(得分:0)

在黑暗中拍摄可能让你感到厌恶的事:

让框架变大,

wx.Frame.__init__(self, parent, -1, title=title, size=(250, 520))

遗漏,也许是不必要的,

#~ self.Fit()

答案 2 :(得分:0)

此代码适用于Ubuntu 16.4,Python 3.5.2,wx.Python 4.0.0b2 gtk2(phoenix

import wx

class MainEdit(wx.Frame):
    def __init__(self, parent=None, title="Main Edit"):
        wx.Frame.__init__(self, parent, -1, title=title)
        panel = MainPanel(self)
        print (3,self.GetScreenRect())

class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        print (1,self.GetScreenRect())
        txt_size = (300, 150)
        txt_style = wx.VSCROLL|wx.HSCROLL|wx.BORDER_SIMPLE|wx.TE_MULTILINE
        self.txt_A = wx.TextCtrl(self, size=txt_size, style=txt_style)
        self.txt_B = wx.TextCtrl(self, size=txt_size, style=txt_style)
        cmd_refresh = wx.Button(self, wx.ID_REFRESH)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.txt_A, flag=wx.ALL, border=10)
        main_sizer.Add(self.txt_B, flag=wx.ALL, border=10)
        main_sizer.Add(cmd_refresh, flag=wx.ALL, border=10)
        self.SetSizerAndFit(main_sizer)
        print (2,self.GetScreenRect())
if __name__ == '__main__':
    app = wx.App()
    frame = MainEdit()
    frame.Show()
    app.MainLoop()

初始屏幕显示为:
enter image description here

尝试上面编辑的代码并观察屏幕尺寸是否适应变化 看它是否给你任何里程 注意:我选择使用parent而不是frame