- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前我正在使用 pycharm 开发 python 网络应用程序。我想用 QT 框架开发桌面应用程序。我已经安装了pyqt。我在 pyqt 中搜索了 hello world 并找到了这个:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
print ('Hello World')
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
但是我不知道把这段代码放在哪里?这是我的 pyqt 设计器:
是否可以告诉我在哪里编写代码以及如何处理按钮点击?
最佳答案
看起来您发布的代码是从 this answer 复制的我的。该代码是一个简单的手写示例,根本不涉及使用 Qt Designer。
使用 Qt Designer 的“Hello World”示例将从这样的 ui
文件开始:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Window</class>
<widget class="QWidget" name="Window">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>171</width>
<height>61</height>
</rect>
</property>
<property name="windowTitle">
<string>Hello World</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="button">
<property name="text">
<string>Test</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
这个文件可以保存为helloworld.ui
并在Qt Designer中打开。
首先要了解 Qt Designer,它不是 IDE——它仅用于设计 GUI,而不是主要的程序逻辑。程序逻辑单独编写,之后连接到GUI。
有两种方法可以做到这一点。第一种是直接加载 ui
文件,使用 uic module :
import sys, os
from PyQt4 import QtGui, QtCore, uic
DIRPATH = os.path.join(os.path.dirname(os.path.abspath(__file__)))
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
uic.loadUi(os.path.join(DIRPATH, 'helloworld.ui'), self)
self.button.clicked.connect(self.handleButton)
def handleButton(self):
print('Hello World')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
这会将 GUI 注入(inject)到本地 Window
类中,该类是与 Qt Designer 中的顶级 GUI 类匹配的子类(在本例中也称为“Window”,但可以是任何东西你喜欢)。其他 GUI 小部件成为子类的属性 - 因此 QPushButton 可用作 self.button
。
将 GUI 与程序逻辑连接起来的另一种方法是使用 pyuic tool从 ui
文件生成 python 模块:
pyuic4 --output=helloworld.py helloworld.ui
然后可以将其导入到主应用程序中:
import sys
from PyQt4 import QtGui, QtCore
from helloworld import Ui_Window
class Window(QtGui.QWidget, Ui_Window):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.button.clicked.connect(self.handleButton)
def handleButton(self):
print('Hello World')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
setupUi
方法继承自生成的Ui_Window
类,与uic.loadUi
做的事情完全一样。
关于python - pyqt 中的世界你好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21538615/
计算时IO (IO ()) , 两个 (IO ())和 ()是计算出来的,所以为什么 main :: IO (IO ()) main = print (print "Hello, World!")
我的球代码在这里:我不知道如何让球撞到墙上时变色。如果我们想在球每次从墙上弹起时随机改变它的颜色该怎么办? //Ball.java import java.awt.Color;
我正在关注官方的 Go 教程:https://golang.org/doc/code.html 但我没有通过命令行做事,而是使用 GoLand。当所有内容都包含在 HelloWorld.go 包中时,
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
这个问题已经有答案了: Is Java "pass-by-reference" or "pass-by-value"? (91 个回答) 已关闭 4 年前。 我已经尝试过像 c(procedure)
我将我的网站作为 centerdownload.net 放在网上,但是当我创建 mysql 数据库时,所有页面的浏览器中都会出现错误: Warning: mysql_connect () [funct
我正在查询数据库。 就我而言,每个用户每天都会有 5 条记录保存在表中。因此,在 10 天内,一个用户将有 50 条记录。我有 50000 个用户,每天的记录数为 50000*5=250000 条记录
在我当前的表中,我有日期列,从该列我可以找到工作日。通过使用 to_timedelta 我创建了 week_start 列,但它没有给出正确的日期。代码如下: final_data['weekday'
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 关于Nginx扩展 以欣宸自己为例,对一个java程序
我想知道bonjour是公共(public)API还是私有(private)API?我们可以直接在我们的应用程序中使用它吗? 最佳答案 Bonjour 由 NSNetServices 和 CFNetS
我编写了以下代码来获取 6 个数字作为输入,然后计算它们的平均值: import java.util.Scanner; class FinalGrade { public static void ma
出现拼写错误并将定价错误地输入为定价 我已经删除了应用程序,删除了数据库中的表,然后让新应用程序从旧应用程序复制了 models.py,我做了备份,那么这里的错误仍然存在是管理面板 这里是 mod
我理解用于确定某件事是否是闰年的代码行,并且我已经看到了用于确定数字总和的代码行,但我不知道如何将两者结合起来。当我运行它时,我收到一条错误消息 In function "main": error.
我在 nodejs 中有一个 api 来获取数据我需要一个 token 授权,但我不知道该怎么做? $(document).ready(function() { $.getJSON("http
教程中的代码有几个问题:ListView Tutorial .给我带来问题的代码 fragment 是: static final String[] COUNTRIES = new String[]
我一直在这个 link 查看 Hello MapView , 但它已经宕机几天了,所以我会在这里问我的问题: 我添加了一个看起来像标记的叠加项。我想根据更新后的位置移动标记。有什么方法可以定位现有标记
我正在尝试位于 here 的教程 我已经为 openCV 编译了示例代码,没有任何问题 - 所以我确定我已经为 opencv 安装了所有必要的东西。我已经将 opencv 库添加到我的项目中,并且我正
错误是:至少对一个 JAR 进行了 TLD 扫描但不包含 TLD。为这个记录器启用调试日志记录,以获得已扫描但未在其中找到 TLD 的 JAR 的完整列表。在扫描期间跳过不需要的 JAR 可以缩短启动
此函数旨在用第一个字符串的过滤结果填充第二个字符串。过滤器应该删除所有特殊字符,只打印所有字母的小写排列 我试过改变“ModifyText”循环的性质,我用指针+while 循环和 for 循环完成了
当我将 .php 文件更改为 .blade.php 时出现错误。在我更改它之前出现此错误。 Route::get('hello','HelloController@index'); 你好 Contro
我是一名优秀的程序员,十分优秀!