简体   繁体   中英

How do I call to a method?

I'm not sure how to make a common utilities file and use the methods within it.

Here is an example utilities file that centres a window on the screen:

class center():
    fg = self.frameGeometry()
    cs = QtGui.QDesktopWidget().availableGeometry().center()
    fg.moveCenter(cs)
    self.move(fg.topLeft())

Here is an example UI:

import os, sys
import puppetUtils
from PyQt4 import QtCore, QtGui


class puppetUI(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.setWindowTitle('Puppet')
        self.setObjectName('PuppetMainWindow')
        self.resize(1280, 720)

        puppetUtils.center(self)

        self.show()



app = QtGui.QApplication(sys.argv)
window = puppetUI()
sys.exit(app.exec_())

What is the correct way to call to puppetUtils.center? I have googled around, tried many ways, but it just doesn't work. This is an example - one of many, that I'd use to have my application function without copy/pasting functions throughout various classes, etc.

Other code I need to implement:

def mouseMoveEvent(self, event):
    #create the mouse 'Grab & Move'
    if self.moving: self.move(event.globalPos()-self.offset)

def mousePressEvent(self, event):
    if event.button() == QtCore.Qt.LeftButton:
        self.moving = True; self.offset = event.pos()


def mouseReleaseEvent(self, event):
    if event.button() == QtCore.Qt.LeftButton:
        self.moving = False

First, change from

class center():

to

def center(var):

because you are actually trying to define a utility function, so it should be a def instead of class .

Then inside the center function, change self to var (or whatever you want to name the function parameter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM