我如何制作一个像这样的 GUI?

发布于 2024-10-09 16:15:41 字数 967 浏览 3 评论 0原文

如果没有插图,这很难解释,所以 - 看哪,这是一个插图,由一些 hello-world 示例的屏幕截图和大量 Paint 工作拼凑而成:

GUI mockup

我已经开始在 .NET 上使用 Windows 窗体(通过 IronPython,但这应该不重要),但还没有弄清楚太多。一般来说,GUI 库非常令人生畏,因为每个类都有很多可能的属性。文档擅长解释一切的作用,但不太擅长帮助您弄清楚您需要什么。

我将动态地组装 GUI,但我不认为这是困难的部分。我现在的症结是:

  • 如何让文本标签自动调整其大小以适应所包含文本的宽度(这样文本就不会被剪切,而且我也不为它们保留不必要的空间)调整窗口大小时)?

  • 如何让垂直滚动条始终出现?设置 VScroll 属性(顺便说一句,为什么当 AutoScroll 是公共时它受到保护?)似乎没有执行任何操作。

  • 当内容垂直布局时(通过Dock = DockStyle.Top),为什么AutoScroll没有添加水平滚动条?我可以使用面板的最小尺寸来防止窗口水平缩小时标签和相应的控件重叠,但是滚动条不会出现并且控件无法访问。

  • 如何在不完全禁用窗口大小的情况下限制窗口大小调整(例如设置最小宽度)? (只需设置表单的最小/最大尺寸?)与此相关的是,有没有办法在不设置最小/最大尺寸的情况下设置最小/最大宽度或高度(即我可以将尺寸限制在一个维度上)? p>

  • 是否有适合十六进制编辑的内置控件,或者我是否必须自己构建一些东西?

...我应该使用其他东西(也许是更强大的东西?)我听说过 WPF,但我知道这涉及 XML,我真的想从 XML 构建一个 GUI - 我已经在对象图中有了数据,并进行某种奇怪的 XML 伪序列化(在 Python 中,同样如此!)以创建 GUI 似乎令人难以置信的迂回。

This is difficult to explain without illustration, so - behold, an illustration, cobbled together from screenshots of a few hello-world examples and a lot of Paint work:

GUI mockup

I have started out using Windows Forms on .NET (via IronPython, but that shouldn't be important), and haven't been able to figure out very much. GUI libraries in general are very intimidating, simply because every class has so many possible attributes. Documentation is good at explaining what everything does, but not so good at helping you figure out what you need.

I will be assembling the GUI dynamically, but I'm not expecting that to be the hard part. The sticking points for me right now are:

  • How do I get text labels to size themselves automatically to the width of the contained text (so that the text doesn't clip, and I also don't reserve unnecessary space for them when resizing the window)?

  • How do I make the vertical scrollbar always appear? Setting the VScroll property (why is this protected when AutoScroll is public, BTW?) doesn't seem to do anything.

  • How come the horizontal scrollbar is not added by AutoScroll when contents are laid out vertically (via Dock = DockStyle.Top)? I can use a minimum size for panels to prevent the label and corresponding control from overlapping when the window is shrunk horizontally, but then the scrollbar doesn't appear and the control is inaccessible.

  • How can I put limits on window resizing (e.g. set a minimum width) without disabling it completely? (Just set minimum/maximum sizes for the Form?) Related to that, is there any way to set minimum/maximum widths or heights without setting a minimum/maximum size (i.e. can I constrain the size in only one dimension)?

  • Is there a built-in control suitable for hex editing or am I going to have to build something myself?

... And should I be using something else (perhaps something more capable?) I've heard WPF mentioned, but I understand that this involves XML and I really want to build a GUI from XML - I already have data in an object graph, and doing some kind of weird XML pseudo-serialization (in Python, no less!) in order to create a GUI seems incredibly roundabout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2024-10-16 16:15:41

如果您愿意使用 Java/Swing,那么基本形式应该非常简单。我想说 Netbeans IDE 有一个非常好的 WYSIWYG GUI 编辑器。尽管这很好,但如果我说仅此而已,那我就是在撒谎。您必须了解 Swing 才能按照您想要的方式进行操作,否则您将陷入困境。它是免费的。您要求的大部分内容都是 GUI 构建器的属性,您至少需要在询问细节之前先查看一下。

我对 Visual Basic 的使用经验很少,该语言的一些问题让我很恼火,但我见过没有什么比将简单的 GUI 结合在一起更容易的了。

If you're willing to use Java/Swing that basic form should be pretty easy. I'd like to say that the Netbeans IDE has a pretty good WYSIWYG GUI editor. Even though it is pretty good I'd by lying if I said that is all there is to it. You have to understand Swing to get things the way you want or you'll be bashing your head against the wall. It's free. Most of what you ask for are properties of the GUI builder, you'll need to at least look before asking specifics.

I've had very little experience with Visual Basic, something about the language aggravated me but there is nothing I've seen easier to slap a simple GUI together with.

芸娘子的小脾气 2024-10-16 16:15:41

我最终使用了 WPF。

从 IronPython 访问功能比我预期的更复杂:(

import clr
clr.AddReferenceByName("PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
clr.AddReferenceByName("PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
clr.AddReferenceByName("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
import System.Windows

我不确定我是否可以/应该更改版本号。PublicKeyToken 大概与某种 DLL 有关出于安全目的进行签名;我不知道为什么 WPF 需要这样做,但 WinForms 不需要。)

由于小部件的命名空间变得非常复杂,并且我想要导入很多名称,因此我设置了一些动态导入代码:

def Import(namespace, what):
    if isinstance(what, str):
        globals()[what] = getattr(namespace, what)
    else:
        for k, v in what.items():
            if k: namespace = getattr(namespace, k)
            for name in v:
                Import(namespace, name)

在找出布局工具的正确组合后(这并不像本摘要听起来那么容易),一切似乎都“正常工作”,我很高兴。滚动行为是通过在主窗口内设置 ScrollViewer 并将 Horizo​​ntalScrollBarVisibility 设置为 Auto 来实现的。在 ScrollViewer 内部,我放置了一个 StackPanel,它堆叠了“字段框架”,每个框架都是一个包含两列的 Grid(我添加了两个默认值) ColumnDefinitionGrid.ColumnDefinitions)。我在第 0 列中设置了“标签”,在第 1 列中设置了“值”小部件(组合框、按钮等),并且 Horizo​​ntalAlignmentRight

我还没有设置嵌套面板,但看起来应该不难。我已经尝试过将事件连接到按钮的系统。

I ended up using WPF.

Getting access to the functionality from IronPython is more involved than I would have expected:

import clr
clr.AddReferenceByName("PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
clr.AddReferenceByName("PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
clr.AddReferenceByName("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
import System.Windows

(I'm not sure if I can/should change the version number. The PublicKeyToken presumably has to do with some kind of DLL signing for security purposes; I don't know why WPF requires this but WinForms didn't.)

Since the namespaces for widgets get quite hairy and there are a lot of names that I want to import, I set up some dynamic import code:

def Import(namespace, what):
    if isinstance(what, str):
        globals()[what] = getattr(namespace, what)
    else:
        for k, v in what.items():
            if k: namespace = getattr(namespace, k)
            for name in v:
                Import(namespace, name)

After figuring out the right combination of layout tools (which wasn't as easy as this summary makes it sound), everything seems to "just work" and I'm quite pleased. The scroll behaviour is accomplished by setting up a ScrollViewer inside the main window and setting the HorizontalScrollBarVisibility to Auto. Inside the ScrollViewer I put a StackPanel which stacks the "field frames", each of which is a Grid with two columns (I add two default ColumnDefinition s to the Grid.ColumnDefinitions). I set the "label" up in column 0, and the "value" widget (combo box, button, whatever) in column 1, with a HorizontalAlignment of Right.

I haven't set up the nested panels yet, but it seems like it shouldn't be difficult. I've already played around with the system for hooking up events to buttons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文