从 .ui 自动完成 [英] Autocomplete from .ui

查看:31
本文介绍了从 .ui 自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 Python3 和 PyQt5 的应用程序,我的 UI 布局开始在 Qt .ui 文件中定义并在运行时加载到我的 QDialog 类中.因此,当加载 UI 时,我的 UI 元素的实例(例如 QPushButton)会自动分配为我的 QDialog 类的实例变量.

I am working on an app using Python3 and PyQt5, with my UI layout begin defined in a Qt .ui file and loaded into my QDialog class at runtime. Therefore, the instances of my UI elements, such as a QPushButton, are automatically assigned as instance variables of my QDialog class when the UI is loaded in.

这样做的问题是,当我使用这些变量来修改元素时,我没有得到任何类型的智能提示或自动完成,因为 Python 和我的 IDE 不知道对象的类是什么.

The problem with this is that when I go to use these variables to modify the elements, I don't get any kind of Intellisense type hinting or autocompletion because Python and my IDE have no clue what the object's class is.

在 Java 中,您可以使用显式缩小强制转换将您的对象强制转换为正确的类型,此时智能感知可以开始提供自动完成功能,因为它知道类型.

In Java, you can use explicit narrowing casting cast your object to the correct type, at which point intellisense can begin providing autocompletion because it knows the type.

例如,在 Java 和 Android SDK 中:

For example, in Java and Android SDK:

TextView name = (TextView) findViewById(R.id.name); 

在这个例子中,findViewById 返回一个 View,所以你可以使用类型转换来确保它被转换为一个 TextView.

In this example, findViewById returns a View, so you can use type casting to ensure that it is cast to a TextView.

在 Python 中,我想做同样的事情,既要确保我访问的 UI 元素是正确的类型,又要能够对实例方法使用自动完成功能.

In Python, I'd like to do the same kind of thing, both to ensure that the UI elements I'm accessing are the correct type and to be able to use autocompletion for instance methods.

这是我目前的情况:

""" 
Because Python doesn't know whether self.my_push_button is an instance 
of QPushButton, this may or may not work, but it won't know until runtime, 
so no autocompletion.
"""
self.my_push_button.setEnabled(False) 

我想要做的是这样的:

( (QPushButton) self.my_push_button ).setEnabled(False) 

我试过了:

QPushButton(self.my_push_button).setEnabled(False)

但据我所知,它复制了原始对象并对新对象执行了setEnabled,这显然不是我想要的.

But from what I can tell, it duplicates the original object and performs setEnabled on the new object, which is obviously not what I want.

我还尝试在 isinstance 函数中使用 assert 语句:

I also tried using assert statements with the isinstance function:

assert isinstance(self.my_push_button, QPushButton)

"""
This works for providing the code completion and checking the type.
However, it only works within the scope of the assert statement, so adding an assert for each variable in each scope in which it is used would be unnecessarily verbose.
"""
self.my_push_button.setEnabled(False) 

<小时>

我知道在 Python 中没有真正的对象转换",但是有什么方法可以在 Python 中使用类似于缩小转换的方法,如上所示,在 Java 中?


I understand that there's not real "casting" with objects in Python, but it there any way to be able to something similar in Python to narrowing casting, as shown above, in Java?

推荐答案

代码完成不像 uic 那样适用于动态生成的元素.

Code completion does not work for dynamically generated elements as uic does.

一种可能的解决方案是使用 pyuic 将 .ui 转换为 .py,然后使用 使用生成的代码.

One possible solution is to convert the .ui to .py using pyuic and then use that class indicated in Using the Generated Code.

这篇关于从 .ui 自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆