wx Python Sizer устанавливает фиксированную высоту панели - PullRequest
0 голосов
/ 26 мая 2020

Я новичок в wx Python и только учусь использовать сайзеры. Я успешно создал панель, которая кажется выровненной правильно, однако я хочу разместить новую панель прямо под ней, и у меня там возникают проблемы. Я хочу, чтобы эта первая панель с зеленым фоном имела фиксированную высоту, но зеленый фон заполняет весь фрейм, даже несмотря на то, что кажется, что содержимое панели выровнено так, как я хочу внутри нее. Спасибо за любую помощь.

Вот изображение с рамкой, развернутой, чтобы заполнить мое окно:

enter image description here

Это всего лишь один панель. Содержимое панели правильно выровнено по верхнему краю, однако я хочу, чтобы панель останавливалась чуть ниже этих виджетов, чтобы я мог начать новую панель. Таким образом, зеленый цвет не должен заполнять все окно, так как я установил для этого фрейма зеленый цвет. зеленый должен остановиться чуть ниже виджетов, затем я хочу добавить еще один фрейм.

Вот мой полный код:

import time
import os
import gc
import wx
import wx.grid

# wxpython app
# read ss from files or live version
class Wx_app(wx.App):

    # __init__ gets called first when any class is initialized
    def __init__(self):

    # intiialize the App window
    super().__init__(clearSigInt=True)

    # don't set size of the frame, just position. let 
    # sizers do the sizing?
    self.frame = MyFrame(None, pos=(10,10))

# frame class (wx python wind)
class MyFrame(wx.Frame):

    def __init__(self, *args, **kwargs):

        # ini the frame and show it
        super(MyFrame, self).__init__(*args, **kwargs)

        # main sizer, will contain all of the other sizers
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)

        # FONTS
        # font to use for the panel in labels, text ctrls, etc
        txt_font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        # font to use for buttons is slightly small than txt_font
        but_font = wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL)

        # PANEL 1
        # first panel is at the top, contains manual pick area plus msg area
        self.pick_area_panel = PickAreaPanel(self, txt_font, but_font)
        self.main_sizer.Fit(self.pick_area_panel)


        # show the frame with panel
        self.Show(True)

# panel class
class PickAreaPanel(wx.Panel):

    def __init__(self, parent, txt_font, but_font):

        # ini
        super().__init__(parent=parent)

        # set background colour for panel
        self.SetBackgroundColour("Green")

        # create the sizer for this panel.
        # stuff is organized horizontally left to right
        self.panel_sizer = wx.BoxSizer(wx.HORIZONTAL)

        # NOTE: By setting width to -1, we tell to go with sizer, and height to 100
        # sets a fixed height
        pick_label = wx.StaticText(self, -1, "Confirm Pick R1 P1: ", size=(240, 25))
        pick_label.SetBackgroundColour("Red")
        pick_label.SetFont(txt_font)

        # input textctrl for the manual pick
        pick_input = wx.TextCtrl(self, -1, size=(320, 25))
        pick_input.SetFont(txt_font)

        # button to confirm manual pick
        pick_confirm_but = wx.Button(self, -1, size=(150, 25))
        pick_confirm_but.SetLabel("Confirm")
        pick_confirm_but.SetFont(but_font)

        # PROG MSG AREA
        # no fixed size, fills rest of panel after previous elements
        # need a nested sizer so can control borders
        # and give it a left borcer thats different from its top border
        prog_msg_area_sizer = wx.BoxSizer(wx.HORIZONTAL)
        prog_msg_area = wx.StaticText(self, -1, "MSG MSG MSG", size=(-1, 25), style=wx.ALIGN_CENTER)
        prog_msg_area.SetBackgroundColour("White")
        prog_msg_area.SetFont(txt_font)
        prog_msg_area_sizer.Add(prog_msg_area, proportion=1, flag=wx.EXPAND|wx.TOP, border=0)

        # add elements to panel sizer
        self.panel_sizer.Add(pick_label, proportion=0, flag=wx.EXPAND|wx.TOP, border=3)
        self.panel_sizer.Add(pick_input, proportion=0, flag=wx.EXPAND)
        self.panel_sizer.Add(pick_confirm_but, proportion=0, flag=wx.EXPAND)
        # prog msg area has its own horiz sizer so can set a left border diff from top border 
        self.panel_sizer.Add(prog_msg_area_sizer, proportion=1, flag=wx.EXPAND|wx.LEFT, border=10)

        # add panel sizer to the main sizer
        # set proporition to 0 so height of panel doesnt change when height of frame changes
        # adds a border around the entire panel
        parent.main_sizer.Add(self.panel_sizer, proportion=0, flag=wx.EXPAND|wx.ALL, border=15)

        # set panel to use the main sizer
        # not sure exactly how this works....
        self.SetSizer(parent.main_sizer)

wx_app = Wx_app()       
if __name__ == '__main__':
    wx_app.MainLoop()

РЕДАКТИРОВАННАЯ ПРОГРАММА С ПРЕДЛАГАЕМЫМИ ИЗМЕНЕНИЯМИ

import time
import os
import gc
import wx
import wx.grid

class Wx_app(wx.App):

    def __init__(self):

    super().__init__(clearSigInt=True)

    self.frame = MyFrame(None, pos=(10,10))

class MyFrame(wx.Frame):

    def __init__(self, *args, **kwargs):

        super(MyFrame, self).__init__(*args, **kwargs)

        self.main_sizer = wx.BoxSizer(wx.VERTICAL)

        txt_font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        but_font = wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL)

        self.pick_area_panel = PickAreaPanel(self, txt_font, but_font)

        # *******ADDED:
        self.SetSizer(self.main_sizer)

        self.Show(True)

# panel class
class PickAreaPanel(wx.Panel):

    def __init__(self, parent, txt_font, but_font):

        super().__init__(parent=parent)

        self.SetBackgroundColour("Green")

        self.panel_sizer = wx.BoxSizer(wx.HORIZONTAL)

        pick_label = wx.StaticText(self, -1, "Confirm Pick R1 P1: ", size=(240, 25))
        pick_label.SetBackgroundColour("Red")
        pick_label.SetFont(txt_font)

        pick_input = wx.TextCtrl(self, -1, size=(320, 25))
        pick_input.SetFont(txt_font)

        pick_confirm_but = wx.Button(self, -1, size=(150, 25))
        pick_confirm_but.SetLabel("Confirm")
        pick_confirm_but.SetFont(but_font)

        prog_msg_area_sizer = wx.BoxSizer(wx.HORIZONTAL)
        prog_msg_area = wx.StaticText(self, -1, "MSG MSG MSG", size=(-1, 25), style=wx.ALIGN_CENTER)
        prog_msg_area.SetBackgroundColour("White")
        prog_msg_area.SetFont(txt_font)
        prog_msg_area_sizer.Add(prog_msg_area, proportion=1, flag=wx.EXPAND|wx.TOP, border=0)


        self.panel_sizer.Add(pick_label, proportion=0, flag=wx.EXPAND|wx.TOP, border=3)
        self.panel_sizer.Add(pick_input, proportion=0, flag=wx.EXPAND)
        self.panel_sizer.Add(pick_confirm_but, proportion=0, flag=wx.EXPAND)
        self.panel_sizer.Add(prog_msg_area_sizer, proportion=1, flag=wx.EXPAND|wx.LEFT, border=10)

        parent.main_sizer.Add(self.panel_sizer, proportion=0, flag=wx.EXPAND|wx.ALL, border=15)

        # CHANGED
        self.SetSizer(self.panel_sizer)

wx_app = Wx_app()       
if __name__ == '__main__':
    wx_app.MainLoop()

1 Ответ

0 голосов
/ 27 мая 2020

Я никогда не пытался установить размер для одного класса как для другого, что вы, кажется, делаете здесь.
Установите размер для панели и отдельно для фрейма. См. Ниже: вы заметите, что я сделал копию панели, чтобы проиллюстрировать 2 панели в рамке.

import time
import os
import gc
import wx
import wx.grid

# wxpython app
# read ss from files or live version
class Wx_app(wx.App):

    # __init__ gets called first when any class is initialized
    def __init__(self):

    # intiialize the App window
        super().__init__(clearSigInt=True)

        # don't set size of the frame, just position. let
        # sizers do the sizing?
        self.frame = MyFrame(None, pos=(10,10))

# frame class (wx python wind)
class MyFrame(wx.Frame):

    def __init__(self, *args, **kwargs):

        # ini the frame and show it
        super(MyFrame, self).__init__(*args, **kwargs)

        # main sizer, will contain all of the other sizers
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)

        # FONTS
        # font to use for the panel in labels, text ctrls, etc
        txt_font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        # font to use for buttons is slightly small than txt_font
        but_font = wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL)

        # PANEL 1
        # first panel is at the top, contains manual pick area plus msg area
        self.pick_area_panel = PickAreaPanel(self, txt_font, but_font)
        self.pick_area_panel2 = PickAreaPanel(self, txt_font, but_font)
        self.main_sizer.Add(self.pick_area_panel, proportion=0, flag=wx.EXPAND|wx.ALL, border=10)
        self.main_sizer.Add(self.pick_area_panel2, proportion=0, flag=wx.EXPAND|wx.ALL, border=10)
        self.SetSizerAndFit(self.main_sizer)

        # show the frame with panel
        self.Show(True)

# panel class
class PickAreaPanel(wx.Panel):

    def __init__(self, parent, txt_font, but_font):

        # ini
        super().__init__(parent=parent)

        # set background colour for panel
        self.SetBackgroundColour("Green")

        # create the sizer for this panel.
        # stuff is organized horizontally left to right
        self.panel_sizer = wx.BoxSizer(wx.HORIZONTAL)

        # NOTE: By setting width to -1, we tell to go with sizer, and height to 100
        # sets a fixed height
        pick_label = wx.StaticText(self, -1, "Confirm Pick R1 P1: ", size=(240, 25))
        pick_label.SetBackgroundColour("Red")
        pick_label.SetFont(txt_font)

        # input textctrl for the manual pick
        pick_input = wx.TextCtrl(self, -1, size=(320, 25))
        pick_input.SetFont(txt_font)

        # button to confirm manual pick
        pick_confirm_but = wx.Button(self, -1, size=(150, 25))
        pick_confirm_but.SetLabel("Confirm")
        pick_confirm_but.SetFont(but_font)

        # PROG MSG AREA
        # no fixed size, fills rest of panel after previous elements
        # need a nested sizer so can control borders
        # and give it a left borcer thats different from its top border
        prog_msg_area_sizer = wx.BoxSizer(wx.HORIZONTAL)
        prog_msg_area = wx.StaticText(self, -1, "MSG MSG MSG", size=(-1, 25), style=wx.ALIGN_CENTER)
        prog_msg_area.SetBackgroundColour("White")
        prog_msg_area.SetFont(txt_font)
        prog_msg_area_sizer.Add(prog_msg_area, proportion=1, flag=wx.EXPAND|wx.TOP, border=0)

        # add elements to panel sizer
        self.panel_sizer.Add(pick_label, proportion=0, flag=wx.EXPAND|wx.TOP, border=3)
        self.panel_sizer.Add(pick_input, proportion=0, flag=wx.EXPAND)
        self.panel_sizer.Add(pick_confirm_but, proportion=0, flag=wx.EXPAND)
        # prog msg area has its own horiz sizer so can set a left border diff from top border
        self.panel_sizer.Add(prog_msg_area_sizer)

        # add panel sizer to the main sizer
        # set proporition to 0 so height of panel doesnt change when height of frame changes
        # adds a border around the entire panel
        #parent.main_sizer.Add(self.panel_sizer, proportion=0, flag=wx.ALL, border=15)

        # set panel to use the main sizer
        # not sure exactly how this works....
        self.SetSizer(self.panel_sizer)

wx_app = Wx_app()
if __name__ == '__main__':
    wx_app.MainLoop()

enter image description here

...