How to avoid mousePressEvent - left click to call paintEvent in PyQt - pyqt

In this program below, I am testing the affect of mousePressEvent:
import sys
from PyQt4 import QtGui, Qt, QtCore
class Test(QtGui.QFrame):
def __init__(self):
QtGui.QFrame.__init__(self)
self.setGeometry(30,30,500, 500)
self.show()
def paintEvent(self, e=None):
print "paintEvent"
qp = QtGui.QPainter()
qp.begin(self)
qp.drawRect(30,30,80,80)
qp.end()
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.RightButton:
print "mousePressEvent- Right"
else:
print "mousePressEvent- Left"
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Test()
sys.exit(app.exec_())
I see that in the first left-click, the frame's paintEvent is called. Is this because when the frame get the focus, it need to be repainted? I wonder if there is any way to avoid paintEvent to be called and every drawing in the frame still intact. The reason is because in my real program, the paintEvent is really heavy, I want to run it as less times as possible.
If that is not possible, is there a way to avoid the frame getting focus when left-click on that?

I don't know whether there are platform-specific differences at play here, but when I try the example code on Linux, there is no paintEvent when clicking on the frame.
This is not surprising really, because, by default, the QFrame is not configured to accept focus of any kind. To get the example to work, I had to explicitly set the focus policy, like this:
class Test(QtGui.QFrame):
def __init__(self):
QtGui.QFrame.__init__(self)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
But maybe the defaults are somehow different on other platforms, and so, to explicitly prevent the frame getting the focus, you might need to do this:
self.setFocusPolicy(QtCore.Qt.NoFocus)

Related

How to setFocus() on QListView in QFileDialog in PyQt5?

I've made my first small GUI program using PyQt5 for some data process for my work.
I have a problem - I can't make QFileDialog (to open file) with the focus on its QListView widget. (In QFileDialog class by default focus is on QLineEdit).
The best solution is to make new class from QFileDialog with changed setFocus settings and unchanged all the other.
It turned out to be not so easy because I didn't find how to adress to inner widgets of QFileDialog.
I'm new in PyQt5, couldn't find any solutionts even for Qt C++.
Thank you for any advices and ideas.
class XFileDialog(QtWidgets.QFileDialog):
"magic code"
file=XFileDialog.getOpenFileName(caption="Open",
filter="FITS (*.fits *.fts *.new)")
It seems that I have found the solution. Perhaps it could be useful for someone.
class XFileDialog(QtWidgets.QFileDialog):
def __init__(self):
QtWidgets.QFileDialog.__init__(self)
self.setDirectory(progdir)
def setVisible(self,v):
super(XFileDialog, self).setVisible(v)
self.setAcceptMode(0)
self.setFileMode(1)
self.setFocusPolicy(11)
self.setNameFilter("All (*) ;; FITS (*.fts *.fits *.new)")
self.focusPreviousChild()
class MyWindow(QtWidgets.QWidget):
def __init__(self,parent=None):
QtWidgets.QWidget.__init__(self,parent)
super().__init__()
self.initUI()
def openFile(self):
global progdir
progdir=QtCore.QDir(os.getcwd())
file=XFileDialog()
file.exec()
.......

QLineEdit.setText only works once in function

I'm currently trying to program for my bachelor thesis. The main part works, so now I want to implement a user interface. I watched some tutorials and worked via trial and error, and my user interface also works. So far so good. But yesterday I changed a small thing and that doesn't do what i want. I have a button saying "start program", and a line-edit where i want to display the current status. My code is:
import sys
from PyQt4 import QtGui
from theguifile import Ui_MainWindow
import otherfile
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.ui.mybutton.clicked.connect(self.runprogram)
def runprogram(self):
self.ui.mylineedit.setText('Running') # doesnt work
try:
otherfile.therealfunction() # works
except ErrorIwanttocatch:
self.ui.mylineedit.setText('thisErrorhappened') # works
else:
self.ui.mylineedit.setText('Success') # works
app = QtGui.QApplication(sys.argv)
window = Main()
sys.exit(app.exec_())
Everything works as I want except from lineedit.setText('Running'). What I want is that "Running" is displayed while otherfile.therealfunction is working. I guess I have to update the line-edit somehow? But until now I didn't figure out how I can do that. I also set the line-edit readonly because I don't want the user to be able to change it, so maybe that's a problem? I thought readonly would only affect what the user can do.
I am using Python3 and PyQt4 with Qt Designer.
Calling otherfile.therealfunction() will block all ui updates until the function completes. You can try forcing immediate ui updates like this:
def runprogram(self):
self.ui.mylineedit.setText('Running')
QtGui.qApp.processEvents()

Pyglet hello world example doesn't show label until a key is pressed

import pyglet
window = pyglet.window.Window()
label = pyglet.text.Label("Hello World!",
font_name="Times New Roman",
color=(255,255,255,255),
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x="center", anchor_y="center")
#window.event
def on_draw():
window.clear()
label.draw()
pyglet.app.run()
This code is taken from the pyglet tutorial at https://pyglet.readthedocs.io/en/pyglet-1.2-maintenance/programming_guide/quickstart.html but when run it doesn't draw the label until any key is pressed. I added the color as I thought the text may have defaulted to black.
Am I missing something really obvious as to why this behaviour is happening?
OK, having had my memory jogged by the comments, I installed the MS Fonts and it now works in python 2.x but I still need to press a key to see the text in python 3. Maybe the font thing is a red-herring and there is some incompatibility with python 3.
As mentioned in the comments.
Most examples found on the internet (even most guides) assume a Windows platform.
If there's a font= declaration with a Windows font but you're running Linux, make sure you got the proper fonts installed or revert to a font you've got installed.
$ fc-list
Also not declaring a font will work too:
label = pyglet.text.Label("Hello World!",
color=(255,255,255,255),
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x="center", anchor_y="center")
Because Pyglet will default to sans-serif:
If you do not particularly care which font is used, and just need to display some readable text, you can specify None as the family name, which will load a default sans-serif font (Helvetica on Mac OS X, Arial on Windows XP)
Your issue is probably that you don't manually update the screen after you drew something. Normally when you press a key, it forces a window.flip(), which basically means update the screen content.
window.clear() also triggers this behavior, but x.draw() does not. Why? Well the thing that takes time in computer graphics is not really the calculations, it's the updating them to the screen that takes time.. There for .draw() doesn't update the screen, it just puts the stuff in the graphical buffer ("page"), you decide when to flip the page and show the new buffer on the screen.
Try this out:
#window.event
def on_draw():
window.clear()
label.draw()
window.flip()
This might be a overkill solution, but it will probably solve the problem.
This overrides the default loop of pyglet and the default draw behavior, it's also one of the classes i use the most in my pyglet projects since it gives me the option to create my own framework.
import pyglet
class Window(pyglet.window.Window):
def __init__(self):
super(Window, self).__init__(vsync = False)
self.sprites = {}
self.sprites['testlabel'] = label = pyglet.text.Label("Hello World!",
color=(255,255,255,255),
font_size=36,
x=self.width//2, y=self.height//2, #self here, being pyglet.window.Window that we've inherited and instanciated with super().
anchor_x="center", anchor_y="center")
self.alive = 1
def on_draw(self):
self.render()
def render(self):
self.clear()
for sprite_name, sprite_obj in self.sprites.items():
sprite_obj.draw()
self.flip()
def on_close(self):
self.alive = 0
def run(self):
while self.alive:
self.render()
# This is very important, this queries (and empties)
# the pyglet event queue, if this queue isn't cleared
# pyglet will hang because it can't input more events,
# and a full buffer is a bad buffer, so we **NEED** this!
event = self.dispatch_events()
win = Window()
win.run()
It's extremely basic, but you can add more sprites, objects and render them in def render(self):.

How do I set the minimum size of a Gtk ButtonBox child?

I'm trying to set the minimum size of the buttons in this GtkButtonBox. Currently they seem to be fixed - approx 85 pixels I think.
Is this possible?
If not, is there another way in Gtk to get two small sized buttons to snuggle together like in the above picture rather than having them appear to be two separate buttons? For example GtkStackSwitcher may be something I could use but there doesn't appear to be a way to respond to click events for a button.
I've used this test program to create the above (Ubuntu 14.04, Gtk+3.10 and Python3):
from gi.repository import Gtk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="example", application=app)
self.set_default_size(350, 200)
self.set_border_width(10)
hbox = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL)
hbox.set_layout(Gtk.ButtonBoxStyle.EXPAND)
button = Gtk.Button(label="a")
hbox.add(button)
button2 = Gtk.Button(label="b")
hbox.add(button2)
self.add(hbox)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
With regards to a question about the desktop environment I'm using.
I've tried Mate, Unity and Gnome-Shell. All work the same way. I've removed the title and those controls. Still the same thing happens. To me this looks more like a GTK issue.
I believe that GtkButtonBox imposes some layout constraints on its buttons that you may not want here. Try using buttons in just a regular GtkGrid, but give them the GTK_STYLE_CLASS_LINKED CSS class.
For each button, do:
button.get_style_context().add_class(Gtk.STYLE_CLASS_LINKED)

Custom sound in a message dialog box in wxPython

I was wondering is there any way to get a custom sound to play as soon as a message dialog box comes up? my only restriction is that I can only use wxPython for this, for arguments sake lets call the sound file 'music.wav' Here is my code so far (ignore the stuff about playing with text, that was me creating a dummy GUI for it to load):
import wx
import time
import winsound, sys
class ButtonTest(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,None,id,'Button/Text test frame',size=(800,500))
panel=wx.Panel(self)
button=wx.Button(panel, label='Exit', pos=(200,50), size=(-1,-1))
self.Bind(wx.EVT_BUTTON, self.closer, button)
self.Bind(wx.EVT_CLOSE, self.wincloser)
ape=wx.StaticText(panel, -1, 'This text is STATIC', (200,80))
ape.SetFont(wx.Font(25, wx.SWISS, wx.ITALIC, wx.BOLD, True,'Times New Roman'))
def beep(sound):
winsound.PlaySound('%s.wav'%sound, winsound.SND_FILENAME)
#wx.FutureCall(1000, beep('C:\Users\Chris\Desktop\music'))
box=wx.MessageDialog(None,'Is this a good test?','Query:',wx.ICON_ERROR)
answer=box.ShowModal()
box.Destroy
beep('C:\Users\Chris\Desktop\music')
def closer(self,event):
self.Close(True)
def wincloser(self,event):
self.Destroy()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=ButtonTest(None,id=-1)
frame.Show()
app.MainLoop()
For Windows, there's winsound, which is built in to Python. Otherwise, you'll need an external library like pyAudio or Snack Sound. See also Play a Sound with Python

Resources