2015-08-23 25 views
0

我有一個主窗口,我叫子窗口(彈出),我無法接入高清錄入(個體經營),它給了我一個屬性錯誤:無法在子窗口存取權限一個「高清」 - 「主窗口對象有沒有屬性‘錄入’」從一個按鈕

AttributeError: 'MainWindow' object has no attribute 'updateTime'

如果我參加了主窗口部分能正常工作,所以我真的不明白是什麼問題。任何幫助將不勝感激。

from PyQt4 import QtGui, QtCore 
from PyQt4 import * 
from PyQt4.QtCore import * 
import sys 

CurrentTime = 0 

class MainWindow(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.central = QtGui.QWidget(self) 
     self.setCentralWidget(self.central) 

     self.hboxPB = QtGui.QHBoxLayout() 
     self.vboxPB = QtGui.QVBoxLayout() 
     self.MyButton = QtGui.QPushButton(self.central) 
     self.MyButton.setText("Push Me") 
     self.hboxPB.addWidget(self.MyButton) 
     self.vboxPB.addLayout(self.hboxPB) 
     self.MyButton.resize(90,90) 
     self.MyButton.clicked.connect(lambda: widgetWindow.start(self)) 

class widgetWindow(QtGui.QWidget): 
    def __init__(self, parent = None): 
     QtGui.QWidget.__init__(self,parent) 
     super(widgetWindow, self).__init__() 
     widgetWindow.start(self) 

    def start(self): 
     window = QtGui.QMainWindow(self) 
     window.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
     CentralWidget = QtGui.QWidget() 
     self.timeSlider = QtGui.QSlider(QtCore.Qt.Horizontal, self) 
     CentralWidgetLayout = QtGui.QHBoxLayout() 
     VBox = QtGui.QVBoxLayout() 
     CentralWidgetLayout.addWidget(self.timeSlider) 
     VBox.addLayout(CentralWidgetLayout) 
     CentralWidget.setLayout(VBox) 
     window.setCentralWidget(CentralWidget) 
     window.show() 

     self.timer = QtCore.QTimer() 
     self.timer.timeout.connect(self.updateTime) 
     self.timer.start(1000) 

    def updateTime(self): 
     global CurrentTime 
     CurrentTime = CurrentTime + 1 
     self.timeSlider.setValue(CurrentTime) 

def main(): 
    app = QtGui.QApplication(sys.argv) 
    win = MainWindow() 
    win.show() 
    win.resize(800,450) 
    sys.exit(app.exec_()) 

if __name__ == '__main__': 
    main() 

回答

-2

試試這個:

from PyQt4 import QtGui, QtCore 
from PyQt4 import * 
from PyQt4.QtCore import * 
import sys 

CurrentTime = 0 
class widgetWindow(QtGui.QWidget): 
    def __init__(self, parent = None): 
     QtGui.QWidget.__init__(self,parent) 
     super(widgetWindow, self).__init__() 
     widgetWindow.start(self) 

    def start(self): 
     window = QtGui.QMainWindow(self) 
     window.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
     CentralWidget = QtGui.QWidget() 
     self.timeSlider = QtGui.QSlider(QtCore.Qt.Horizontal, self) 
     CentralWidgetLayout = QtGui.QHBoxLayout() 
     VBox = QtGui.QVBoxLayout() 
     CentralWidgetLayout.addWidget(self.timeSlider) 
     VBox.addLayout(CentralWidgetLayout) 
     CentralWidget.setLayout(VBox) 
     window.setCentralWidget(CentralWidget) 
     window.show() 

     self.timer = QtCore.QTimer() 
     self.timer.timeout.connect(self.updateTime) 
     self.timer.start(1000) 

    def updateTime(self): 
     global CurrentTime 
     CurrentTime = CurrentTime + 1 
     self.timeSlider.setValue(CurrentTime) 



class MainWindow(QtGui.QMainWindow,widgetWindow):#here add subclass 
    def __init__(self, parent=None): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.central = QtGui.QWidget(self) 
     self.setCentralWidget(self.central) 

     self.hboxPB = QtGui.QHBoxLayout() 
     self.vboxPB = QtGui.QVBoxLayout() 
     self.MyButton = QtGui.QPushButton(self.central) 
     self.MyButton.setText("Push Me") 
     self.hboxPB.addWidget(self.MyButton) 
     self.vboxPB.addLayout(self.hboxPB) 
     self.MyButton.resize(90,90) 
     self.MyButton.clicked.connect(lambda: widgetWindow.start(self)) 



def main(): 
    app = QtGui.QApplication(sys.argv) 
    win = MainWindow() 
    win.show() 
    win.resize(800,450) 
    sys.exit(app.exec_()) 

if __name__ == '__main__': 
    main() 

你有錯誤命名空間。您必須確保您使用適當的名稱空間,否則解釋器不知道查找課程的適當位置。

+0

非常感謝你!!!!你不知道你剛剛救了我多少時間。 – user3723727

+0

@geochet thnx進行編輯! – dsgdfg

2

您的問題與行開始

self.MyButton.clicked.connect(lambda: widgetWindow.start(self)) 

讓我們去建造這是什麼做的。

  1. 你有一個按鈕,self.MyButtonMainWindow的一個實例,在其中存在(實例化的main()功能類)

  2. 您正在連接到該按鈕的clicked信號。

  3. 連接到這個信號的功能是lambda函數調用在班上widgetWindow的功能。請注意,這與調用類實例的方法截然不同。你沒有在這裏實例化一個類(你沒有創建一個對象)。你是說,使用方法定義start在類widgetWindow但要操作的對象self,其中self是類MainWindow的實例上。

希望你現在開始看到你所做的問題。而不是創建widgetWindow類的實例,您嘗試使用的方法從widgetWindow,如果它是的MainWindow的方法。我建議你閱讀更多關於面向對象的Python編程,以便在你仍然遇到麻煩時(尤其是如果你不清楚類和對象之間的區別),請仔細閱讀本文。

因此,解決方案是創建的widgetWindow一個實例(而不是直接訪問類的方法),和您的按鈕連接到該實例的方法。我已經修改了您的代碼,以便在下面發佈。我評論過我已更改的部分。如果您對我所做的事有疑問,請告訴我。

from PyQt4 import QtGui, QtCore 
from PyQt4 import * 
from PyQt4.QtCore import * 
import sys 

CurrentTime = 0 

class MainWindow(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.central = QtGui.QWidget(self) 
     self.setCentralWidget(self.central) 

     self.hboxPB = QtGui.QHBoxLayout() 
     self.vboxPB = QtGui.QVBoxLayout() 
     self.MyButton = QtGui.QPushButton(self.central) 
     self.MyButton.setText("Push Me") 
     self.hboxPB.addWidget(self.MyButton) 
     self.vboxPB.addLayout(self.hboxPB) 
     self.MyButton.resize(90,90) 

     # instantiate the widgetWindow (pass this window as the parent) 
     self.widgetwindow = widgetWindow(self) 
     # connect the button to the start method of this instance we created above 
     self.MyButton.clicked.connect(self.widgetwindow.start) 

# no need to subclass QWidget here. This is just a wrapper class to hold your main window 
class widgetWindow(object): 
    def __init__(self, parent = None): 
     # Store the parent for use later 
     self.parent = parent 

    def start(self): 
     # change the window parent to be the parent we stored in the __init__ method 
     window = QtGui.QMainWindow(self.parent) 
     window.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
     # Add a signal to stop the timer when the window is destroyed 
     window.destroyed.connect(self.stopTimer) 
     CentralWidget = QtGui.QWidget() 
     # Change the parent to be the window we just created 
     self.timeSlider = QtGui.QSlider(QtCore.Qt.Horizontal, window) 
     CentralWidgetLayout = QtGui.QHBoxLayout() 
     VBox = QtGui.QVBoxLayout() 
     CentralWidgetLayout.addWidget(self.timeSlider) 
     VBox.addLayout(CentralWidgetLayout) 
     CentralWidget.setLayout(VBox) 
     window.setCentralWidget(CentralWidget) 
     window.show() 

     self.timer = QtCore.QTimer() 
     self.timer.timeout.connect(self.updateTime) 
     self.timer.start(1000) 

    def updateTime(self): 
     global CurrentTime 
     CurrentTime = CurrentTime + 1 
     self.timeSlider.setValue(CurrentTime) 

    # Method to stop the timer once the window is destroyed 
    def stopTimer(self): 
     self.timer.stop() 

def main(): 
    app = QtGui.QApplication(sys.argv) 
    win = MainWindow() 
    win.show() 
    win.resize(800,450) 
    sys.exit(app.exec_()) 

if __name__ == '__main__': 
    main() 
相關問題