【问题标题】:Small square on upper left corner of window when panel is switched wxpython切换面板时窗口左上角的小方块wxpython
【发布时间】:2021-04-03 12:21:37
【问题描述】:

我遇到了 wx.panels 的问题。 在我的项目中创建了一个文件,其中 wx.frame 在文件中的一个类中继承,其他两个文件继承了 wx.panels。 我试图隐藏/销毁 panel1 并调用 panel2 但不知何故它不起作用。开关上会出现一个带有面板背景颜色的小方形图标。 我在stackoverflow上提到了[this topic][1],但我发现如果我不使用sizer,代码就不起作用(在不同的文件中拆分时。) [1]:https://stackoverflow.com/questions/58794753/wxpython-panel-is-cropped-with-only-a-small-box-shown-at-the-top-left-hand-corne

main.py

import sys
import wx
from test1 import panel1
from test2 import panel2

class main(wx.Frame):
    def __init__(self,*args, **kwargs):
        super().__init__(*args, **kwargs)
        self.panel = panel1(parent=self)
        self.Show()
        # display=wx.Display()
        # size = display.GetGeometry().GetSize()
        # print(size)

    def update_window(self):
        self.panel2=panel2(self,wx.ID_ANY)
        self.panel2.Show()
        self.panel.Hide()
        self.Refresh()

if __name__ == "__main__":
    print(__file__)
    app = wx.App()  # Create a new app, don't redirect stdout/stderr to a window.
    frame = main(None, wx.ID_ANY, "Hello World",size=(1024,700)) # A Frame is a top-level window.
    frame.Show(True)     # Show the frame.
    app.MainLoop()

test1.py

import wx
from view.panel import custom_panel
from test2 import panel2

class panel1(custom_panel):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.parent=parent
        self.btn1 = wx.Button(self,wx.ID_ANY,"button1",pos=(100,50),size=(300,50))
        self.Bind(wx.EVT_BUTTON, self.On_btn1_clicked, self.btn1)


    def On_btn1_clicked(self, event):
        self.parent.update_window()

test2.py

import wx
from view.panel import custom_panel

class panel2(custom_panel):
    def __init__(self,parent=None,id=None,size=(1024,700)):
        super().__init__(parent)
        self.parent=parent
        self.btn2 = wx.Button(self,wx.ID_ANY,"button2",pos=(300,50),size=(300,50))
        self.Bind(wx.EVT_BUTTON, self.On_btn2_clicked, self.btn2)


    def On_btn2_clicked(self, event):
        self.parent.update_window()


custom_panel.py

import wx

class custom_panel(wx.Panel):
    def __init__(self,*args, **kwargs):
        super().__init__(*args, **kwargs)
        self.SetBackgroundColour("#000000")


if __name__ == "__main__":
    pass
    

【问题讨论】:

  • 请分享您用来处理问题的代码。
  • 请立即查看帖子@AlexK。

标签: python user-interface wxpython wxwidgets


【解决方案1】:

@PavanHebli,

我强烈建议你学习 sizer 以及如何使用它们。

虽然您的解决方案确实有效,但它是非常糟糕的解决方案。

【讨论】:

    【解决方案2】:

    嗯,我也同意使用sizer。

    但是:我也不得不解决很长时间的问题并且不知道解决方案。尺寸调整器不起作用(如我所料)

    对我来说,问题是面板没有(或尺寸不正确)。 解决方案是eiter:

    panel.Fit() 要么 panel.SetSize(x,y)

    【讨论】:

      【解决方案3】:

      解决了! 在创建第二个面板时,我只传递了父级,我猜 wx.DefaultSize 与第一个面板不同,它不适用于第二个面板。 我所要做的就是在 test2.py 文件的 init 方法中传递 size=(1024,700) ,如下所示:

      super().__init__(parent,id=id,size=(1024,700))
      

      就是这样。

      【讨论】:

      • 不要这样做。在我能想到的任何水平上,这都是非常糟糕的。只需根据需要使用 sizer 和 Hide() 和 Show() 面板。看在上帝的份上,永远不要像那样明确地传递大小,就像永远永远一样。
      猜你喜欢
      • 2011-07-06
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多