pyqt icon overlap with word - pyqt

This is what happens:
code here:
self.connectAction = createAction(
self, "设备连接(&C)", self.setupDevice,
icon_id = QStyle.SP_DialogNoButton)
and this the createAction:
def createAction(parent,
text,
slot=None,
shortcut=None,
icon=None,
tip=None,
checkable=False,
signal="triggered()",
whatis=None,
icon_id=None):
action = QAction(text, parent)
if icon:
if isinstance(icon, QIcon):
action.setIcon(icon)
else:
action.setIcon(QIcon(":/%s.png" % icon))
if icon_id:
action.setIcon(app.style().standardIcon(icon_id))
if slot:
connect(action, signal, slot)
return action

OK, I found my problem, I set qt stylesheet, and it cause this problem.

Related

Error using checkmouse

I'm trying to use checkmouse in order to undraw something from my window. When someone clicks the button it should undraw the text and write something else. I'm using checkMouse and getX() and getY() to do this but i keep receiving this error that states:
File "C:\Users\User\Documents\python\project2.py", line 71, in panel
if (clicknew.getX()>90 and clicknew.getX()<210) and (clicknew.getY()>35 and clicknew.getY() < 0):
AttributeError: 'NoneType' object has no attribute 'getX'
this code that i have done so far is as follows:
from graphics import *
#creating the game panel window
def panel():
#grey window, with coordinates flipped, with banners etc
win = GraphWin("Start Panel", 300,200)
win.setCoords(0,0,300,200)
win.setBackground("light grey")
#drawing the BoilerMazer banner with text
boilermazer = Rectangle(Point(0,200),Point(300,160))
boilermazer.setFill("white")
boilermazer.draw(win)
#text inside
banner1 = Text(Point(150,180),"BoilerMazer")
banner1.setStyle("bold")
banner1.setSize(20)
banner1.draw(win)
#initial game panel is going to have two buttons and a top scores object
#top score "screen"
toprec = Rectangle(Point(60,140),Point(240,50))
toprec.setFill("white")
toprec.draw(win)
#text inside toprec
topscores = Text(Point(150,130),"TOP SCORES")
topscores.setSize(8)
topscores.draw(win)
border = Text(Point(150,120),"======")
border.draw(win)
bigmac = Text(Point(150,110),"Big Mac 21")
bigmac.setSize(8)
bigmac.draw(win)
tt = Text(Point(150,90),"T.T 23")
tt.setSize(8)
tt.draw(win)
cshell = Text(Point(150,75),"C-Shell 25")
cshell.setSize(8)
cshell.draw(win)
macmac = Text(Point(150,55),"MacMac 27")
macmac.setSize(8)
macmac.draw(win)
#new player button that will eventually be clicked
new1 = Point(90,35)
new2 = Point(210,0)
newrec = Rectangle(new1,new2)
newrec.setFill("chartreuse2")
newrec.draw(win)
#new player button text
newplayer = Text(Point(150,18),"NEW PLAYER")
newplayer.draw(win)
#reset button
resetrec = Rectangle(Point(240,35),Point(300,0))
resetrec.setFill("red")
resetrec.draw(win)
#resettext
reset = Text(Point(270,18),"RESET")
reset.draw(win)
#secondary panel window is the game panel after they click new player
#set up points that we check between for the new player button first
#setting up the checkmouse
clicknew = win.checkMouse()
if (clicknew.getX()>90 and clicknew.getX()<210) and (clicknew.getY()>35 and clicknew.getY() < 0):
newplayer.undraw()
you can find the graphics window here:http://mcsp.wartburg.edu/zelle/python/graphics.py
I don't understand what I'm doing wrong, is there some other method that I'm supposed to be using? Thanks for your help
According to the docs, checkMouse() returns None if no mouse click has been detected priorly. So that seems to be the case.
You could put a loop around the call to checkMouse and keep checking if clicknew is not None and only in that case go on in your program. But maybe there's a better way...
UPDATE:
Example:
while True:
clicknew = win.getMouse()
if clicknew:
break
else:
time.sleep(0.1) # avoid busy waiting
# clicknew is set now => use it

pyside qdialog box or qaction icon is closing app

The issue is a combination of setting an icon on the action in the toolbar (or qpushbutton) and showing a qmessagebox when triggered. If I remove the icon, the message box displays. And if I remove the message box but keep the icon, the app stays open. The other odd thing is, if I add the icon to the push button but not the action, and click on the action, it still closes the app. The doubly odd thing is if I add main.qpush_button_clicked before qapplication.exec_(), the message box is displayed. However, the next time I click on either, it closes the app.
I have looked at multiple posts and some of the ideas were to use setQuitOnLastWindowClosed, but that did not fix the issue. I also implemented event to see what was happening. When I click on either of the items, it triggers a ChildAdded event, then closes.
Also, this only does not work when I use cx_Freeze on a Mac. I have not tried on Win. It works properly when run using Eclipse or from CLI.
Does anyone have any ideas on what might be causing this, or how to fix it besides not using icons.
The icon I used is from Android icon pack.
I can add the crash log if you need it.
class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.qicon = QIcon('../ic_add_black_24dp_1x.png')
self.tool_bar = self.addToolBar('main')
qaction = QAction(self)
self.tool_bar.addAction(qaction)
qaction.setText('add')
qaction.setIcon(self.qicon)
qaction.triggered.connect(self.qpush_button_clicked)
qpush_button = QPushButton('add')
self.setCentralWidget(qpush_button)
qpush_button.setIcon(self.qicon)
qpush_button.clicked.connect(self.qpush_button_clicked)
def qpush_button_clicked(self, *args, **kwargs):
QMessageBox.critical(self, 'test', 'testing')
if __name__ == '__main__':
qapplication = QApplication(sys.argv)
main = Main()
main.show()
main.raise_()
qapplication.exec_()
And here is the setup file
name = 'dialog'
version = '0.1'
description = 'description'
packages = ('os',)
excludes = ('tkinter',)
include_files = ('ic_add_black_24dp_1x.png',)
build_exe = dict(packages=packages,
excludes=excludes,
include_files=include_files)
options = dict(build_exe=build_exe)
base = 'Win32GUI' if sys.platform == 'win32' else None
script = 'dialog.py'
executables = (Executable(script, base=base),)
setup(name=name,
version=version,
description=description,
options=options,
executables=executables)
PySide Version : 1.2.2
PySide Component: : (1, 2, 2, 'final', 0)
PySide Compiled Qt : 4.8.7
PySide Qt Component: (4, 8, 7)
Running Qt Version : 4.8.7

python : bind function to button

this is my python code GUI generated from PAGE 4.6.
I want to create a function which will change the textbox value in real time example
tbCapturedImage.set("test 1").
take a look at
self.btnCapture.bind('<Button-1>', self.Capture_a)
but it cant seems to change the value of the textbox.
self.tbCapturedImage = Text(self.TLabelframe1)
self.tbCapturedImage.place(relx=0.04, rely=0.65, relheight=0.33
, relwidth=0.93)
self.tbCapturedImage.configure(background="white")
self.tbCapturedImage.configure(font="TkTextFont")
self.tbCapturedImage.configure(selectbackground="#c4c4c4")
self.tbCapturedImage.configure(width=206)
self.tbCapturedImage.configure(wrap=WORD)
self.btnCapture = Button(master)
self.btnCapture.place(relx=0.01, rely=0.92, height=45, width=982)
self.btnCapture.configure(activebackground="#d9d9d9")
self.btnCapture.configure(disabledforeground="#a7a4a7")
self.btnCapture.configure(font=font11)
self.btnCapture.configure(text='''Capture Image''')
self.btnCapture.bind('<Button-1>', self.Capture_a)
self.Labelframe1 = LabelFrame(master)
self.Labelframe1.place(relx=0.25, rely=0.48, relheight=0.43
, relwidth=0.74)
self.Labelframe1.configure(relief=GROOVE)
self.Labelframe1.configure(text='''Color Detection''')
self.Labelframe1.configure(width=740)
self.Labelframe2 = LabelFrame(master)
self.Labelframe2.place(relx=0.25, rely=0.05, relheight=0.43
, relwidth=0.35)
self.Labelframe2.configure(relief=GROOVE)
self.Labelframe2.configure(text='''Perspective Transformation''')
self.Labelframe2.configure(width=350)
self.Labelframe3 = LabelFrame(master)
self.Labelframe3.place(relx=0.61, rely=0.05, relheight=0.43
, relwidth=0.38)
self.Labelframe3.configure(relief=GROOVE)
self.Labelframe3.configure(text='''Haar-Like Detection''')
self.Labelframe3.configure(width=380)
def Capture():
tbCapture.Set("test")
def Capture_a(self,event):
self.Capture()
if __name__ == '__main__':
vp_start_gui()
You have to use self and set instead of Set
And probably you should use tbCapturedImage instead of tbCapture
def Capture(self): # self
self.tbCapturedImage.set("test") # self, set and tbCapturedImage
BTW: you can use command= in Button instead of bind
self.btnCapture = Button(master, command=self.Capture)
or
self.btnCapture.configure(command=self.Capture)
command= doesn't send event so method doesn't need argument event

How to manually set the toggle state of wxpython platebutton

I am building an application using platebutton iin wxpython. The problem is that I am not able to manually SetState of the toogle buton. I used SetState(0) but it does not change the state of toggle button. Any help would be great. Thanks. Sample code:
self.infinity= platebutton.PlateButton(self._ribbon,wx.ID_NEW, bmp = wx.Bitmap('infinity.bmp'), pos = (0,0), size = (38,18), style= platebutton.PB_STYLE_NOBG |platebutton.PB_STYLE_TOGGLE)
def OnInfinityToggled(self,event):
if event.GetEventObject().IsPressed():
self.popupmenu = wx.Menu()
Session = self.popupmenu.Append(-1, "Session")
self.Bind(wx.EVT_MENU, self.SessionMenu, Session)
self.PopupMenu(self.popupmenu,(2,23))
else:
pass
def SessionMenu(self, event):
print 5
self.infinity.SetState(0)
self.infinity.Raise()
PLATE_NORMAL = 0
PLATE_PRESSED = 1
PLATE_HIGHLIGHT = 2
SetState(0) means set to normal.
Here's how I managed to toggle state:
btn._ToggleState()
btn._pressed = True
I had the same problem. Playing around I managed to resolve my problem with
button._SetState(ix)
button.Refresh()
where ix = your choice of state.

How do I create a critical error message using PySide?

I seem to be hitting a brick wall. No matter what I do, creating a critical error Message Box just doesn't seem to be working. Here's what I've tried thus far:
flags = QtGui.QMessageBox.StandardButton.Abort
flags |= QtGui.QMessageBox.StandardButton.Ignore
result = QtGui.QMessageBox.critical(
self,
'CRITICAL ERROR',
'Error Message',
flags
)
As taken from this tutorial (old I know, but it's been helpful thus far). Doing this however, brings up the following error:
'PySide.QtGui.QMessageBox.critical' called with wrong argument types:
PySide.QtGui.QMessageBox.critical(CreateMessage, str,
StandardButtons)
Supported signatures:
PySide.QtGui.QMessageBox.critical(PySide.QtGui.QWidget, unicode,
unicode, PySide.QtGui.QMessageBox.StandardButtons = QMessageBox.Ok,
PySide.QtGui.QMessageBox.StandardButton = NoButton)
PySide.QtGui.QMessageBox.critical(PySide.QtGui.QWidget, unicode,
unicode, PySide.QtGui.QMessageBox.StandardButton,
PySide.QtGui.QMessageBox.StandardButton)
I've also tried the following:
result = QtGui.QMessageBox.critical(
self,
'CRITICAL ERROR',
'Error Message',
QtGui.QMessageBox.StandardButton.Abort
)
# Or this....
result = QtGui.QMessageBox.critical(
self,
'CRITICAL ERROR',
'Error Message',
QtGui.QMessageBox.Abort
)
None of these seem to work properly. How do I create a critical error message box?
Simple Example Below
import sys
from PySide import QtGui
app = QtGui.QApplication(sys.argv)
a=QtGui.QMessageBox.critical(None,'Error!',"Error Message!", QtGui.QMessageBox.Abort)
Here's an example from Qt.Gitorious.
from PySide import QtGui, QtCore
import sys
class Dialog(QtGui.QDialog):
MESSAGE = QtCore.QT_TR_NOOP("<p>Message boxes have a caption, a text, and up to "
"three buttons, each with standard or custom texts.</p>"
"<p>Click a button or press Esc.</p>")
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.criticalLabel = QtGui.QLabel()
self.criticalLabel.setFrameStyle(QtGui.QFrame.Sunken | QtGui.QFrame.Panel)
self.criticalButton = QtGui.QPushButton(self.tr("QMessageBox.critica&l()"))
layout = QtGui.QGridLayout()
layout.addWidget(self.criticalButton, 10, 0)
layout.addWidget(self.criticalLabel, 10, 1)
self.setLayout(layout)
self.connect(self.criticalButton, QtCore.SIGNAL("clicked()"), self.criticalMessage)
def criticalMessage(self):
reply = QtGui.QMessageBox.critical(self, self.tr("QMessageBox.showCritical()"),
Dialog.MESSAGE, QtGui.QMessageBox.Abort|
QtGui.QMessageBox.StandardButton.Retry|
QtGui.QMessageBox.StandardButton.Ignore)
if reply == QtGui.QMessageBox.Abort:
self.criticalLabel.setText(self.tr("Abort"))
elif reply == QtGui.QMessageBox.Retry:
self.criticalLabel.setText(self.tr("Retry"))
else:
self.criticalLabel.setText(self.tr("Ignore"))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog = Dialog()
sys.exit(dialog.exec_())
To answer your question you can check the documentation:
static PySide.QtGui.QMessageBox.critical(parent, title, text[, buttons=QMessageBox.Ok[, defaultButton=NoButton]])
In the example, parent = self, title = self.tr("QMessageBox.showCritical()"), text = Dialog.MESSAGE, buttons = QtGui.QMessageBox.Abort | QtGui.QMessageBox.StandardButton.Retry | QtGui.QMessageBox.StandardButton.Ignore
The tr is just some Qt function to set up translations, basically its a string. I can't really tell you what you did wrong, looking at the error message, it seems to have parsed things wrong. Possibly because of the way you assigned values to flags.
The example also shows how to deal with the result of the critical dialog, which seems useful.

Resources