I'm using wxPython DirDialog and it seems to have a bug.
When launching the dialog I specify a default path (defaultPath).
That path is being selected by the dialog but the dialog is not scrolled to the selected path.
Instead the dialog is scrolled to the top of the dialog.
This leaves the user to scroll A LOT down to reach the default path.
Very inconvenient.
Any way to correct this?
Using:
Python 2.6.5
wxPython 2.8.12.1
Windows 8.1
It may not be any consolation but with Python 2.7.12 Wx '3.0.2.0 gtk2 (classic)' on Linux, the following works as it should. Check if you are doing something different.
def OnSelect_dir(self,event):
dialog = wx.DirDialog (None,defaultPath=self.client_dir.GetValue(), message = 'Pick a directory.' )
if dialog.ShowModal() == wx.ID_OK:
self.client_dir.SetValue(dialog.GetPath())
else:
pass
dialog.Destroy()
It works is I hard code defaultPath='/home/rolf' as well.
Well apparently if I exclude the style "wx.DD_DEFAULT_STYLE" then it works just fine.
So this works:
style = wx.DD_DIR_MUST_EXIST
But this doesn't focus the dialog properly on the defaultPath:
style = wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST
I guess it must be a bug somewhere
Related
I just installed Python3 (3.5.2) and Pyqt5 (5.8.2) and I am following this tutorial to learn and make a GUI: http://zetcode.com/gui/pyqt5/firstprograms/
I'm trying to run the 2nd example but program is returning an error (which also happened on the 1st one, but since it had no image i took no notice) which is the following:
QApplication: invalid style override passed, ignoring it.
No XVisualInfo for format QSurfaceFormat(version 2.0, options QFlags<QSurfaceFormat::FormatOption>(), depthBufferSize -1, redBufferSize 1, greenBufferSize 1, blueBufferSize 1, alphaBufferSize -1, stencilBufferSize -1, samples -1, swapBehavior QSurfaceFormat::SwapBehavior(SingleBuffer), swapInterval 1, profile QSurfaceFormat::OpenGLContextProfile(NoProfile))
Falling back to using screens root_visual.
What is the meaning of this? Am i missing some packages?
I installed pyqt first with this command:
sudo -H pip3 install PyQt5
but Python3 was not acknowledging its existence so i searched the apt ubuntu repos and installed with:
sudo apt install python3-PyQt5
I also tried to reference the image by full path /foo/bar/image.png and nothing
What is the problem?
EDIT #1
The code that i am using is from example 2:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
This example shows an icon
in the titlebar of the window.
author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""
import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
base_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(base_dir)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('image.png'))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
After your post i reinstalled all my packages. The error is slightly different but the result is the same:
python3 example_02.py
QApplication: invalid style override passed, ignoring it.
Screencapture:
Notice that you are having no icons at all for all applications, not just for the PyQt icon example. This is because by default, certain environments turn off the icons in the titlebar. You have to enable them.
For instance in Xfce Desktop Environment, we can use the xfce4-settings-editor tool. In Settings/Settings Editor select xfwm4.
Find the show_app_icon option and check it. Change a theme back and forth to see the changes; they are not visible right away.
After this, you will see the icon in the titlebar of the PyQt5 example.
As for the warning; it is a recent thing and it has to do something
with the incopatibilities between Qt and GTK theming. I have not found
a solution to remove the warning so far.
So first off, you have no errors in your code. That's more akin to a warning but not even. What the following line is telling you
QApplication: invalid style override passed, ignoring it
is that your style option is invalid. If that were an error your script wouldn't run at all.
What I see right off the bat is this, you never supply a path to your image.
Now if the image is in the same root directory as the script then it should recognize said image without a path. But if you're attempting to do what I think you are it wouldn't work like that anyway. I think you're trying to create a launcher icon as well as a title bar icon, which typically goes hand in hand.
It appears to me that you've added it to Atom as some form of resource file. In which case most Ide's create a path for that file. Sometimes it's a path, other times a local url. QT its self does both when working with the QT creator.
I've never used Atom so I can't tell you how that works.
What I can say is this. you're using Linux which means .ico files are useless. I told you before linux doesn't handle icon files the same way windows does. This is most likely your problem.
So I sugesst you take a look at this
https://askubuntu.com/questions/476981/how-do-i-make-a-desktop-icon-to-launch-a-program
After you read that take a look at this if you have to https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles#Using_a_text_editor
Both of those links explain how to create a launcher icon for your program.
The following link will explain how to set the icon on the menu bar (title bar) in your program.
PyQt4 set windows taskbar icon
I hope this helps you out!
I study PyQt5 from author who give this question,also I have this problem that my icons can't show,I try some ways to catch it,that's what I do,hope it works!
First, it's important that you should use absolute path of the icons,for example:
self.setWindowIcon(QIcon("F:/Workspace/PyQT5-Study/images/web.png"))
but this not a good idea,so you can use second way like this:
from PyQt5.QtCore import QFileInfo
# ...
def initUI(self):
# ...
root = QFileInfo(__file__).absolutePath()
self.setWindowIcon(QIcon(root+'/images/web.png'))
# or
# import os
# current_dir = os.path.dirname(os.path.realpath(__file__))
# self.setWindowIcon(QIcon(os.path.join(current_dir, 'images/web.png')))
# ...
Last, if your icons also can't show, you should check this icon, if it's a legal icon.
In short, the normal images are unlimited so more, they can store many images and transform easily, but the icons have sure size,color kind,and more important,the icons have transparency, that means you can see the background, they have frame(not always straight). So you can use the web online tools to transform your image and try again,that really help me!
Also you should check the icon's source format, ensure you never change it, like .jpg to .png,and other. This will produce problem!
Wish you can solve the problem!
On windows be sure to use a real .ico file and a full path
iconpath = os.path.join(os.getcwd(),'qtlearning','assets','python.ico')
self.setWindowIcon(QIcon(iconpath))
I faced the exact same problem.
First things first. There is no setWindowIcon() method under QWidget or QMainWindow classes, in fact. you should be trying to set the QIcon at the Application level as follows.
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('home.png'))
Second, the icon thus created using this code does not reflect on the title of the window, instead it will reflect as an application icon as shown in the image below. the home.png
" icon for Application" in Ubuntu and not the " icon over the Window Title"
Finally, the path does not really matter, it can be an absolute path or a relative path, the system will consider either.
i just provided the full path of icon as simple as that
I have a python script for GUI using wxpython. It works perfectly fine in Windows, however, when I run the script in OS X, the toolbar is now shown (I installed wxpython from its official website and used the cocoa version, and I am using OS X 10.10 and python 2.7). Following is the part of the code regarding the toolbar:
self.toolBar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL|wx.TB_FLAT|wx.TB_DOCKABLE)
self.myChoice = ComboBoxWithHelp(self.toolBar, wx.NewId(), size=(200, -1), value=..., choices=..., style=wx.CB_DROPDOWN,)
self.toolBar.AddControl(self.myChoice)
iconname = 'icons/new.png'
self.toolBar.AddSimpleTool(1, wx.Image(iconname, wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'New', 'New')
...
self.toolBar.Realize()
self.SetToolBar(self.toolBar)
Nothing is shown below the menu bar, however the space is left there. Did I installed the wxpython wrongly or use the function wrongly?
By the way, the above code also works for Ubuntu.
What is self in this code? Toolbars are a little different on OSX, and and can be a bit tricky, so there may be some issues if self is not a wx.Frame or a class derived from wx.Frame. This is because the native toolbars are actually part of the frame rather than an independent widget. It should be switching to a non-native toolbar if the parent is not the frame, but then you'll need to manage its size and layout yourself.
If self is already the frame then you may want to try not specifying the style flags and let it just use the default style, or you can try creating the toolbar like this instead and let the frame create it:
self.toolBar = self.CreateToolBar()
I have some Python code which works well in Windows and Ubuntu Linux, but has a problem on Mac.
What I am doing is presenting a user (within my app) the ability to choose an application he wants opened whenever he presses a button on my app (so that he can invoke without having to search for it every time)
The setup code goes like this (simplified):
self.app_opt = options = {}
options['title'] = 'Please choose an app from your computer'
options['initialdir'] = '~/'
chosen_app = "~/"
chosen_app = askopenfilename(parent = self.parent, **self.app_opt)
self.chosen_app = chosen_app
Later on, the button is pressed and the code in the button looks like this:
subprocess.Popen(self.chosen_app)
As I said, it works fine in Windows (I go over to "Program Files", select an executable and all is fine), it runs when I push the button. Also in Ubuntu - (I select from say /usr/bin) and app runs fine when I push the button.
Tried to do this in Mac - just as an example we would like to open iTunes when pressing the button - Now the real app to run iTunes is found at e.g.
/Applications/iTunes.app/Contents/MacOS/iTunes
But I can't select deeper than
/Applications/iTunes.app
Is there some option/setting I need to put in the file dialog to make this work?
After asking a friend (thanks DJ!) It seems I was going about this the wrong way
In Mac I should not be calling
subprocess.Popen(self.chosen_app)
but something more like
subprocess.Popen('open -a \"' + self.chosen_app + '\"')
as explained here
Will report back on how this works
Python 2.7.3 x64
wxPython 2.8 x64
I'm having trouble changing the font of the wxpython message dialog box. I'd like to use a fixed-width font (I think wxFAMILY_MODERN is) to control the formatting of the output. Here's the code I'm using to test with ...
def infDialog (self, msg, title):
""" Display Info Dialog Message """
font = wx.Font(14, wx.MODERN, wx.NORMAL, wx.NORMAL)
style = wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP
dialog = wx.MessageDialog(self, msg, title, style)
dialog.CenterOnParent()
dialog.SetFont(font)
result = dialog.ShowModal()
if result == wx.ID_OK:
print dialog.GetFont().GetFaceName()
dialog.Destroy()
return
# End infDialog()
But the results when I click OK are always "Arial". Any thoughts? Perhaps I need to make a custom dialog class?
Thanks,
-RMWChaos
The wx.MessageDialog is a wrapper around the OS / system message dialog. I suspect each OS only allows so much editing or none at all. So yes, using a custom wx.Dialog or the generic message dialog widget (wx.lib.agw.genericmessagedialog) is the way to go if fonts are important.
I was looking all over the internet and found that no one actually took the time to answer the question, instead just telling users to create their own dialog class. There is actually an easy way in which you should be able to change the font of your message dialog. Grabbing the dialog's textCtrl object and then setting the font of that object should do the trick.
Simply replace:
dialog.SetFont(font)
with
txt_ctrl = dialog.text
txt_ctrl.SetFont(font)
Let me know if this works for you.
I'd like to have my application display an icon in OSX menu bar (top of screen where Growl sits). How would I do this using Python? (I understand this is not possible using wxPython but I am not after a wxPython specific solution).
Thanks!
An implementation of this may be found at:
https://web.archive.org/web/20080709014939/http://the.taoofmac.com/space/blog/2007/04/22/1745
http://the.taoofmac.com/space/blog/2007/04/22/1745
The API for displaying icons in the OS X menubar is called NSStatusItem. It's going to be difficult or impossible to use from a wxPython application, though -- you will probably have to write your application using PyObjC to use it effectively.
The rumps package makes this very easy. Here's an example from rumps's README:
import rumps
class AwesomeStatusBarApp(rumps.App):
#rumps.clicked("Preferences")
def prefs(self, _):
rumps.alert("jk! no preferences available!")
#rumps.clicked("Silly button")
def onoff(self, sender):
sender.state = not sender.state
#rumps.clicked("Say hi")
def sayhi(self, _):
rumps.notification("Awesome title", "amazing subtitle", "hi!!1")
if __name__ == "__main__":
AwesomeStatusBarApp("Awesome App").run()