gpt4 book ai didi

wxPython StaticText 转义嵌套的 BoxSizer

转载 作者:行者123 更新时间:2023-12-04 09:56:01 25 4
gpt4 key购买 nike

全面披露:我是 wxpython 的新手(大约 3 天),我不太了解 boxsizers 的细节。

我正在编写一个 GUI 来显示照片和一些相关的细节。我正在使用 box sizers 在主选项卡上水平显示 3 个“部分”。最左边的项目是图像预览,中间的项目显示一些信息,最右边的有一个 StaticText 标题和两个单选按钮。不知何故,StaticText 似乎正在逃避其 BoxSizer。我为不同的部分涂上了花哨的颜色以使其更加明显:
escaped panel item

这是代码的相关部分:

class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.loadContents()

def loadContents(self):
self.loadPreview()
self.loadImageInfo()
self.loadColorOptions()

row0 = wx.BoxSizer(wx.HORIZONTAL)
row0.Add(self.preview, 1, wx.LEFT, 5)
row0.Add(self.imageInfo, 1, wx.EXPAND , 5)
row0.Add(self.colorPanel, 1, wx.EXPAND, 5)

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(row0)
self.SetSizer(sizer)

def loadColorOptions(self):
panel = wx.Panel(self)
panel.SetBackgroundColour((230,230,230))
self.title = wx.StaticText(self, label="Options", style=wx.TE_READONLY | wx.TE_MULTILINE)
self.radioButtonColor = wx.RadioButton(panel, label="Color", style=wx.RB_GROUP)
self.radioButtonGray = wx.RadioButton(panel, label="Grayscale")

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.title, 0, wx.ALL | wx.EXPAND, 5)
sizer.Add(self.radioButtonColor, 0, wx.ALL, 5)
sizer.Add(self.radioButtonGray, 0, wx.ALL, 5)
panel.SetSizer(sizer)

self.colorPanel = panel

以及完整的上下文:

import wx 
from wx.lib.wordwrap import wordwrap
import os

from mock_utils import *

ICON_STR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'flower.ico')
INITIAL_SIZE_X = 1000
INITIAL_SIZE_Y = 700
SCALE_RATIO = 100
PREVIEW_SIZE = [4*SCALE_RATIO, 3*SCALE_RATIO] # 4:3
TITLE="Photo GUI"

class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.loadContents()

def loadContents(self):
self.loadPreview()
self.loadImageInfo()
self.loadColorOptions()

row0 = wx.BoxSizer(wx.HORIZONTAL)
row0.Add(self.preview, 1, wx.LEFT, 5)
row0.Add(self.imageInfo, 1, wx.EXPAND , 5)
row0.Add(self.colorPanel, 1, wx.EXPAND, 5)

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(row0)
self.SetSizer(sizer)

def loadColorOptions(self):
panel = wx.Panel(self)
panel.SetBackgroundColour((230,230,230))
self.title = wx.StaticText(self, label="Options", style=wx.TE_READONLY | wx.TE_MULTILINE)
self.radioButtonColor = wx.RadioButton(panel, label="Color", style=wx.RB_GROUP)
self.radioButtonGray = wx.RadioButton(panel, label="Grayscale")

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.title, 0, wx.ALL | wx.EXPAND, 5)
sizer.Add(self.radioButtonColor, 0, wx.ALL, 5)
sizer.Add(self.radioButtonGray, 0, wx.ALL, 5)
panel.SetSizer(sizer)

self.colorPanel = panel

def loadPreview(self):
fname = getImage()
self.imageInfo = getImageInfo(fname)
if fname is not None:
img = wx.Image(fname, wx.BITMAP_TYPE_ANY)
img = img.Scale(PREVIEW_SIZE[0],PREVIEW_SIZE[1], wx.IMAGE_QUALITY_HIGH)
else:
img = wx.Image(PREVIEW_SIZE)

self.preview = wx.StaticBitmap(self, wx.ID_ANY, wx.Bitmap(img))

def loadImageInfo(self):
imgInfo = wx.StaticText(self, style=wx.TE_READONLY | wx.TE_MULTILINE)
infoStr = wordwrap("Filename:\n" + "\t" + self.imageInfo.get("name") + \
"\nSettings:\n" + "\t" + "Exposure: " + self.imageInfo.get("exposure") + \
"\n" + "\t" + "Focus: " + self.imageInfo.get("focus"), 350, wx.ClientDC(self))

imgInfo.SetLabel(infoStr)
imgInfo.SetBackgroundColour("yellow")
self.imageInfo = imgInfo

class Panel2(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)

class PhotoGuiFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title=TITLE, size=(INITIAL_SIZE_X,INITIAL_SIZE_Y))
self.SetIcon(wx.Icon(ICON_STR, wx.BITMAP_TYPE_ICO, 16, 16))
self.Center() # center gui on screen

self.loadPanels()

def loadPanels(self):

p = wx.Panel(self)
dock = wx.Notebook(p)
main_panel = MainPanel(dock)
panel2 = Panel2(dock)

dock.AddPage(main_panel, "Tab1")
dock.AddPage(panel2, "Tab2")

sizer = wx.BoxSizer()
sizer.Add(dock, 1, wx.EXPAND)
p.SetSizer(sizer)


if __name__ == "__main__":
app = wx.App()
frame = PhotoGuiFrame().Show()
app.MainLoop()

如果您想要一个最小的“工作”示例(将显示一个空白框而不是加载图像),那么还有一个简单的 mock_utils.py:

pth1 = "/home/wxnewbie/Desktop/gui/test/images/img"

def getImage():
return None

def getImageInfo(fname):
return {"name": pth1+"2.tif", "exposure":"1.5ms", "focus":"automatic"}

我完全不知道为什么 StaticText 会逃避 BoxSizer。我唯一能想到的是我需要摆脱它所连接的面板(在这种情况下,我是否应该归还 sizer 本身?)。但是我随后遇到了 sizer 中的项目完全消失的麻烦,所以我想我只是缺少一些关于 sizer 应该如何工作的基本信息。

最佳答案

这是一个经典,不能只见树木不见森林,问题。
您正在放置 self.titleself不在面板中。

这应该可以解决问题。
self.title = wx.StaticText(panel, label="Options", style=wx.TE_READONLY | wx.TE_MULTILINE)

关于wxPython StaticText 转义嵌套的 BoxSizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61924187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com