设置 QLabel 的全局位置
我正在尝试使用 PyQt 在固定位置显示 QLabel(没有父级)。 QLabel 的功能是只出现一会儿的图形,默认情况下它出现在屏幕中间,但我找不到如何移动其位置。
有什么建议吗??
更新:
这是我的代码,也许我这样做的方式有问题:
from PyQt4.QtCore import Qt,QTimer
from PyQt4.QtGui import QLabel,QPixmap,QApplication
import os
import subprocess
class BrightnessControl(QLabel):
def __init__(self,parent=None):
self.__command__ = "nvclock -S%s"
self.__imgPath__ = "../../../../ui/alfredozn_vaio/brightness/img/"
self.__image__ = QPixmap()
super(BrightnessControl,self).__init__(parent,Qt.SplashScreen)
#self.loadImg(100)
def comman(self):
return self.__command__
def image(self):
return self.__image__
def imgPath(self):
return self.__imgPath__
def currentValue(self):
'''
Obtenemos el valor actual del smartdimmer
'''
res=subprocess.Popen(["smartdimmer","-g"],stdout=subprocess.PIPE)
return int(res.stdout.readline().split()[-1])
def loadImg(self,value):
'''
Aquí asumimos que el único medio de control será la aplicación, por lo que los valores se moverán en múltiplos de 10
'''
os.system(self.comman() % str(value))
self.image().load("%s%i.png" % (self.imgPath(),value))
self.setPixmap(self.image())
self.setMask(self.image().mask())
self.update()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
try:
if len(sys.argv) < 2 :
raise ValueError
else:
val=10
if sys.argv[1] == "down":
val*=-1
elif sys.argv[1] != "up":
raise ValueError
form = BrightnessControl()
val += form.currentValue()
val = 20 if val < 20 else 100 if val > 100 else val
form.loadImg(val)
#This move function is not working
form.move(100,100)
form.show()
QTimer.singleShot(3000,app.quit)
app.exec_()
except ValueError:
print "Modo de uso: SonyBrightnessControl [up|down]"
I'm trying to display a QLabel (without parent) at a fixed position using PyQt. The QLabel functions as a graphic that appears only for a moment, it's appears in the middle of the screen by default but I can't find how to move its position.
Any suggestion??
updated:
This is my code, maybe is a problem in the way I'm doing this:
from PyQt4.QtCore import Qt,QTimer
from PyQt4.QtGui import QLabel,QPixmap,QApplication
import os
import subprocess
class BrightnessControl(QLabel):
def __init__(self,parent=None):
self.__command__ = "nvclock -S%s"
self.__imgPath__ = "../../../../ui/alfredozn_vaio/brightness/img/"
self.__image__ = QPixmap()
super(BrightnessControl,self).__init__(parent,Qt.SplashScreen)
#self.loadImg(100)
def comman(self):
return self.__command__
def image(self):
return self.__image__
def imgPath(self):
return self.__imgPath__
def currentValue(self):
'''
Obtenemos el valor actual del smartdimmer
'''
res=subprocess.Popen(["smartdimmer","-g"],stdout=subprocess.PIPE)
return int(res.stdout.readline().split()[-1])
def loadImg(self,value):
'''
Aquí asumimos que el único medio de control será la aplicación, por lo que los valores se moverán en múltiplos de 10
'''
os.system(self.comman() % str(value))
self.image().load("%s%i.png" % (self.imgPath(),value))
self.setPixmap(self.image())
self.setMask(self.image().mask())
self.update()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
try:
if len(sys.argv) < 2 :
raise ValueError
else:
val=10
if sys.argv[1] == "down":
val*=-1
elif sys.argv[1] != "up":
raise ValueError
form = BrightnessControl()
val += form.currentValue()
val = 20 if val < 20 else 100 if val > 100 else val
form.loadImg(val)
#This move function is not working
form.move(100,100)
form.show()
QTimer.singleShot(3000,app.quit)
app.exec_()
except ValueError:
print "Modo de uso: SonyBrightnessControl [up|down]"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 pos 属性,也可以调用 move() 将其移动到标签中。但它并不总是有效。根据我的经验,最好的方法是使用 label.setGeometry() 函数。
如果你想让它在屏幕中间,代码应该是这样的(是c++代码,对不起,我不知道python):
如果你在父级的resizeEvent中执行此操作,你将使其始终位于屏幕的中心。
您还可以优化将 sizeHint 保存到变量中的代码,以免多次调用它。
You can use pos property or you can move it calling move() into the label. But it doesn't always work well. The best method from my experience is to use label.setGeometry() function.
If you want it in the middle of the screen the code should be like this (Is c++ code, I dont know python sorry):
If you do this into the resizeEvent of the parent you will have it always in the center of it.
You can also optimize the code saving the sizeHint into a variable to not call it several times.
使用其
pos
属性。Use its
pos
property.