如何使单个 PyQt 代码在 Windows 和 Linux 上运行?
PyQt 专家:我在 Windows 中开发了 GUI,并使用 setGeometry
来定位小部件。当我尝试在 Linux 中运行相同的代码时,它看起来很混乱。
另外,在 Windows 中,8 号字体看起来不错。但在Linux中,尤其是Ubuntu中,由于默认字体大小为10,因此显示效果不佳。其中一些差异是组框的边框在 Linux 中不出现,而在 Windows 中可见。
有没有一种方法可以让我使用相同的代码在 Windows 和 Linux 中获得相同的外观和感觉,而不管字体和大小的变化以及其他差异?
将来,如果我将应用程序移植到 Mac,相同的代码也能在那里工作吗?或者我是否应该通过检查 platform.system()
等于“windows”或“linux”来维护每个单独的代码?
PyQt experts: I developed the GUI in Windows and used setGeometry
to position the widgets. When I tried to run the same code in Linux it looks cluttered.
And added to that in Windows the font size of 8 seems good. But in Linux, especially in Ubuntu, it doesn't appear well since the font size is 10 by default. Some among the differences are the border of the group box doesn't appear in Linux while it is visible in Windows..
Is there a way that I can make the same code to get the same look and feel in Windows and Linux irrespective of the font and size changes and other differences?
In future if I port my application to Mac will the same code work there too? Or should I have to maintain the separate code for each by checking with platform.system()
equal to "windows" or "linux"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案很简单:不要直接使用 setGeometry (来定位您的小部件)。
考虑以下问题:如果用户想要调整应用程序窗口的大小怎么办?
在 QSplitters(如果您想要在两个组件之间调整大小句柄)和/或 QVBoxLayouts / QHBoxLayouts(请注意,这些可以嵌套)中编写用户界面(您可以从设计器或代码中执行此操作)。
这将使您的 UI 组件表现一致。
The answer is simple: don't use setGeometry directly (to position your widgets).
Consider the following: what if the user wants to resize your application window?
Compose the user interface (you could do this from Designer or from code) within QSplitters (if you want a resize handle between two components) and/or within QVBoxLayouts / QHBoxLayouts (note that these can be nested).
This will make your UI components behave consistently.
我同意@ChristopheD。使用 setGeometry 是不好的。这就像设计一个具有固定像素几何形状的网页,然后想知道为什么它在其他设备上看起来很糟糕。
Qt 有很多精彩的布局代码。让它完成它的工作。
默认情况下,Qt 将根据 QStyle 中包含的说明绘制小部件。您可以很容易地测试不同样式破坏布局的严重程度...使用不同样式选项运行程序。就像这样:
program.py 风格的主题
还可以尝试
-style Platinum
或-style windows
。即使不同版本的 Windows 也可能会破坏您的布局。如果您确实想了解基于像素的布局有多糟糕,请尝试使用 -reverse 参数运行您的程序...对于使用从右到左语言(例如希伯来语或波斯语)的人来说,这就是您的程序的外观。
小部件未在您想要的位置绘制的问题可以通过为小部件创建自定义绘制代码来解决。请参阅 PyQt QPainter 文档 或更好的原始文档Qt QPainter 文档。
虽然我希望我的答案有用,但这可能意味着您的程序需要部分重写。然而,从长远来看,这意味着您将拥有可在样式和操作系统之间移植的代码,甚至可以进行翻译(假设您关心这一点)。
I agree with @ChristopheD. Using setGeometry is bad. It's like designing a webpage with fixed pixel geometry and then wondering why it looks bad on another device.
Qt has a lot of wonderful layout code. Let it do it's job.
Qt by default will paint a widget according to instructions contained in the QStyle. You can test how badly you break your layout in different styles easily enough... run your program with different style options. Like so:
program.py -style motif
Also try
-style platinum
or-style windows
. Even different versions of Windows will probably break your layout.If you really want to see how bad pixel-based layouts are, try running your program with the -reverse parameter... that's how your program will look to someone running it who speaks a Right-To-Left language, like Hebrew or Farsi.
The problem that you have with widgets not drawing where you want them to can be solved by creating custom painting code for your widget. See the PyQt QPainter docs or better yet, the original Qt QPainter docs..
While I hope my answer is useful, it probably means your program needs to be partially rewritten. In the long term, however, it means that you'll have code that is portable between styles and operating systems, and will even work translated (assuming you care about that).