首先,对不起我的英语。我的母语是西班牙语。 所以,我正在做这个小项目来学习python。我正在学习如何使用 pyqt5 做 ui。这个应用程序很简单,它有三个输入、一个按钮和一个输出。 我在此应用程序中使用 mvc 软件模式,并且我的视图、模型和控制器位于单独的文件中。
问题: 在控制器类中,我将唯一的按钮连接到名为 (_calculate) 的插槽。当我运行应用程序并按下该按钮时,终端应该打印一条文本,以便我可以查看它是否正常工作。终端没有向我显示任何内容。 尝试不同类型的事情后,我发现如果我在视图类中执行相同的绑定,则会执行 _calculate 。我做了一个关于使用 pyqt5 的计算器的教程。本教程中的计算器使用 mvc 工作得很好,所以我用它来查明我是否忘记或错过了某些内容,但没有出现任何明显的情况。
我的控制器类
class controller: def __init__(self, view): self._view = view self._connectsignals() def _connectsignals(self): self._view.button.clicked.connect(self._calculate) def _calculate(self): print('trying to calculate')
我的视图类
from pyqt5.qtwidgets import qmainwindow from pyqt5.qtwidgets import qvboxlayout from pyqt5.qtwidgets import qhboxlayout from pyqt5.qtwidgets import qwidget from pyqt5.qtwidgets import qlabel from pyqt5.qtwidgets import qlineedit from pyqt5.qtwidgets import qspaceritem from pyqt5.qtwidgets import qsizepolicy from pyqt5.qtwidgets import qpushbutton from pyqt5.qtwidgets import textedit from pyqt5.qtgui import qpixmap from pyqt5.qtcore import qt from toolcontroller import controller class userinterface(qmainwindow): def __init__(self): super().__init__() self.setwindowtitle('bdo tool') self.setfixedsize(450, 300) self._centralwidget = qwidget(self) self.setcentralwidget(self._centralwidget) self._createwindowskeleton() def _createwindowskeleton(self): # vertical container who contains all the program widget self._generallayout = qvboxlayout() self._centralwidget.setlayout(self._generallayout) self._generallayout.setalignment(qt.aligncenter) self._generallayout.addlayout(self._createfirstrow()) self._generallayout.addlayout(self._createbutton()) self._generallayout.addwidget(self._createareatext()) def _createfirstrow(self): hlayout = qhboxlayout() spacer = qspaceritem(20, 20, hpolicy=qsizepolicy.expanding) self._inputboxes = { self.input_base_fail: (qpixmap(self.img_base_fails), qlineedit()), self.input_target_fail: (qpixmap(self.img_target_fail), qlineedit()), self.input_stack_amount: (qpixmap(self.img_stack_amount), qlineedit()), } keys = list(self._inputboxes.keys()) for key, value in self._inputboxes.items(): pixmap, editline = value label = qlabel() label.setpixmap(pixmap) editline.setfixedwidth(40) editline.setalignment(qt.alignright) hlayout.addwidget(label) hlayout.addwidget(editline) if key != keys[-1]: hlayout.addspaceritem(spacer) return hlayout def _createbutton(self): self.button = qpushbutton('calculate') spacer = qspaceritem(20, 20, hpolicy=qsizepolicy.expanding) hlayout = qhboxlayout() hlayout.addspaceritem(spacer) hlayout.addwidget(self.button) hlayout.addspaceritem(spacer) return hlayout def _createareatext(self): self._infodisplay = qtextedit() self._infodisplay.setenabled(false) return self._infodisplay input_base_fail = 1 input_target_fail = 2 input_stack_amount = 3 img_base_fails = 'img\\user25x25.png' img_target_fail = 'img\\target25x25.png' img_stack_amount = 'img\\stack25x25.png'
我的主要内容
import sys from PyQt5.QtWidgets import QApplication from ToolView import UserInterface from ToolController import Controller def main(): app = QApplication(sys.argv) view = UserInterface() view.show() Controller(view=view) sys.exit(app.exec()) if __name__ == '__main__': main()
正确答案
问题是您没有为控制器实例创建持久对象,因此该实例随后会立即被垃圾收集,因为它没有在其他任何地方引用。
只要存在对实例的引用,它就会按预期工作。
在这种情况下,一个局部变量就足够了,因为 app.exec()
将阻止 main
内的进一步处理,确保实例将存在直到它存在。
def main(): app = QApplication(sys.argv) view = UserInterface() view.show() controller = Controller(view=view) sys.exit(app.exec())
尽管如此,让我告诉您,虽然从概念上来说使用 mvc 通常是一个好主意,但您应该谨慎使用它,不要“夸大”模式,并且只有在它确实有助于开发时才使用它。
我想指出其中一个mvc 的缺点:
我知道您的示例是一个简单且概念性的示例,但就其用途而言,它确实是一个过于复杂的示例。例如:
- 您似乎没有使用模型,或者至少您似乎不需要完整的 mvc 模式来做到这一点(只要有实际优势,就应该选择 mvc)这样做,而不是“因为你应该使用它”);
- 使用 mvc 模式并不一定意味着您必须使用 3 个类(和 3 个单独的文件),这也可能会降低代码的可导航性; mvc 主要是关于程序逻辑中这三个元素的划分方式:qt(大多数提供 ui 元素的框架)已经对此有所帮助,因为它提供了除了显示自身之外几乎不执行任何操作的 ui 元素,以及允许交互的其他元素与它们(文件访问、网络接口和实际模型);
- 过度使用函数来创建 ui 通常是一个坏主意,因为它使事情变得比实际情况复杂得多,最重要的是从可读性的角度来看:虽然使用单独的函数可能有助于为了保持代码“更整洁”,还创建了函数以实现可重用性,并且在
userinterface
类中,有 4 个函数肯定只会使用一次。
以上是PyQt5:連接到點擊訊號的插槽不起作用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中