I know this is very simple, just use the command self.set_icon_from_file("icon.png"), however my program still does not display the icon. I made sure the icon.png is in the same working directory as the Python file. I also tried giving the complete file path, but still it does not display the icon.
I am using Ubuntu 10.10 if that helps and using Python V2.6. I use Glade Interface Designer to design the GUI. However, I tried setting the icon both using Glade and using the command above.
I hope I have provided sufficient information.
EDIT: I got the status icon to work in my program.. However in the question I meant the program icon displayed in the task bar and also on the left side of the application bar.
I made sure the icon.png is in the same working directory of the python file.
This may be your problem — paths are looked up relative to the working directory of the Python interpreter, not the file containing the code. I often find myself defining a function like:
def get_resource_path(rel_path):
dir_of_py_file = os.path.dirname(__file__)
rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
abs_path_to_resource = os.path.abspath(rel_path_to_resource)
return abs_path_to_resource
Mine isn't actually quite that verbose, but hopefully the variable names make it clear what's going on. Also, getting the absolute path isn't strictly necessary, but might help if you need to debug.
Then you can just do:
self.set_icon_from_file(get_resource_path("icon.png"))
Update: Here is a demo program. "icon.png" is in the same directory as this script, and I run it using ./gtktest.py. I see the icon in the top left corner (standard place for my theme). icon.png is just a shape drawn in Inkscape and exported as a bitmap (it works with the original SVG too, anyway).
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class HelloWorld:
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
# create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_icon_from_file('icon.png')
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
# Creates a new button with the label "Hello World".
self.button = gtk.Button("Hello World")
self.window.add(self.button)
self.button.show()
self.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
hello = HelloWorld()
hello.main()
I am not sure what icon you are creating, but try this smallest PyGTK icon-showing example of taskbar icon I have thought of:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
# create icon object
statusIcon = gtk.StatusIcon()
# load it
statusIcon.set_from_file("icon.ico")
# show it
statusIcon.set_visible(True)
# and run main gtk loop
gtk.main()
Maybe you just missed the command statusIcon.set_visible(True)
For standard icons, use stock items, and find icons that suits your needs. that way
You don't have to pack icons whith your program
The icons change
according to the user's theme and will blend nicely in her
environment.
for pyGTK :
gtk.icon_theme_get_default().load_icon("folder-open", 128, 0)
Related
I am using PyQt5 QFileDialog.getOpenFileName. I am expecting the box to remain open until the "open" button is clicked. However, when I run the code on my Linux system, the dialog box closes immediately when the file name is clicked. On a Windows system, the box behaves as expected and remains open until the 'Open' button is clicked. The results are the same with or without the QFileDialog.DontUseNativeDialog option set.
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog
import sys
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QFileDialog Test")
button = QPushButton("Click to open file")
button.setCheckable(True)
button.clicked.connect(self.open_file)
# Set the central widget of the Window.
self.setCentralWidget(button)
def open_file(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file_name, _ = QFileDialog.getOpenFileName(None, "Open File",
"", "Python Files (*.py);;Text Files (*.txt)",options=options)
app = QApplication(sys.argv)
window = Main()
window.show()
app.exec_()
Edit:
I logged out of KDE and started an Openbox session instead, then ran the above code. QFileDialog behaved as I was expecting, and waited for me to click the Open button. This verifies the problem exists with KDE / KWin, and that the code, run under other window managers, will likely work fine.
Still isn't a real solution, but I am more informed now than I was earlier.
2nd Edit:
I found that if I change Workspace Behavior -> General Behavior -> Click Behavior from Single click, to Double click, my QFileDialog issue goes away. How to get around this would be a different topic though.
3rd Edit:
Added 'QFileDialog.DontUseNativeDialog' option to sample code.
It seems like Qt tries to respect the way the OS opens files and folders in its file manager, even when using the native dialog. That depends on the SH_ItemView_ActivateItemOnSingleClick style hint, and the only way to bypass it is to apply a proxy style.
While you could apply the style to the QFileDialog's view within its __init__ (as long as you're using the native dialog), you're using static methods, so you can only do this by setting the style to the whole QApplication.
Note that, unlike stylesheets, palette and font, styles are not propagated to children widgets, and they always use the QApplication style (or the style manually set for them).
class SingleClickWorkaroundProxy(QProxyStyle):
def styleHint(self, hint, option, widget, data):
if hint == self.SH_ItemView_ActivateItemOnSingleClick:
return False
return super().styleHint(hint, option, widget, data)
# ...
app = QApplication(sys.argv)
app.setStyle(SingleClickWorkaroundProxy())
window = Main()
window.show()
app.exec_()
I am writing a python app using Tkinter and I cannot find a way to change the default title Tk in OS top bar (Gnome top bar and dock too).
I can set the main window title but it does not change the string in the top bar where it stays Tk. Honestly I am surprised I cannot find anyone asking this same question.
I am developing on Ubuntu 18.04 using Python 3.6
This is a sample code to replicate the problem:
import tkinter as tk
if __name__ == "__main__":
mainWindow = tk.Tk()
mainWindow.title("TEST")
mainWindow.geometry("1024x600")
mainWindow.mainloop()
I would expect to read the string TEST in the main window frame title, in the OS top bar and in the OS application dock, but I get it only in the windows frame title. The rest display Tk
Use className
mainWindow = tk.Tk(className='Ghanshyam')
According to python Docs on tkinter:
The Tk class is instantiated without arguments. This creates a toplevel widget of Tk which usually is the main window of an application. Each instance has its own associated Tcl interpreter.
The top bar title in Ubuntu is not set by the application running, it is set by the desktop entry for the app. I'm not sure why it works the way it does when there is no desktop entry present, but you can create a file like this and import it into Ubuntu for it to work properly.
[Desktop Entry]
Type=Application
Terminal=false
Icon=/path/to/icon/icon.png
Name=TITLE (this is what handles the top bar title)
Exec=/path/to/file/executable
Categories=Utility;
See this question on AskUbuntu.
I am experiencing different behaviour on the same code using the python console and a python script.
The code is as follows:
import gtk
import webkit
win = gtk.Window()
win.show()
web = webkit.WebView()
win.add(web)
web.show()
web.open("http://www.google.com")
When running the code in the python console, the output is a new frame that contains the google main page.
When running the code as a script, the result is a void frame. It closes very fast but even if I use a delay function, the webkit is not added to the frame.
How is it possible?
Furthermore, using PyDev IDE it flags: "unresolved import: gtk",
but if i run the project, the program starts without problem of compilation. is it normal?
Add
gtk.main()
to the end of your script. This starts the gtk event loop.
import gtk
import webkit
class App(object):
def __init__(self):
win = gtk.Window()
win.connect("destroy", self.destroy)
web = webkit.WebView()
web.open("http://www.google.com")
win.add(web)
web.show()
win.show()
def destroy(self, widget, data = None):
gtk.main_quit()
app = App()
gtk.main()
My guess is that the console keeps the python session open, while at the end of the script the program closes. When the script closes, it takes everything it created with it.
Something to test this theory: if you type "exit" in the console do you see the interface shut down in the same manner? If so, think of some code (e.g. a pause like a raw_input) that will allow the script to stay open.
Good luck!
I'm working on an applcation in Python's PyQt4 and cannot find how to change the taskbar icon. I made my .ui files in Qt's Designer, where I can change the windowIcon properties. But that is not what I am looking for. I want to change the look of the application's icon in windows taskbar. For now it is Python logo in a window icon.
I found some information on SO: link but it's not helping me much.
I tried:
app = QtGui.QApplication([])
app.setWindowIcon(QtGui.QIcon('chip_icon_normal.png'))
app.exec_()
But the icon remains unchanged.
What i want to change, showing the picture:
(This is done calling the setWindowIcon on main window/ dialog, or the application, as shown above.)
This problem is caused by some peculiarities in how taskbar icons are handled on the Windows platform.
See this answer for details, along with a workaround using ctypes.
It seems to me that the problem may be caused by lack of icon with the right size.
The following setup worked for me in PyQT4:
# set app icon
app_icon = QtGui.QIcon()
app_icon.addFile('gui/icons/16x16.png', QtCore.QSize(16,16))
app_icon.addFile('gui/icons/24x24.png', QtCore.QSize(24,24))
app_icon.addFile('gui/icons/32x32.png', QtCore.QSize(32,32))
app_icon.addFile('gui/icons/48x48.png', QtCore.QSize(48,48))
app_icon.addFile('gui/icons/256x256.png', QtCore.QSize(256,256))
app.setWindowIcon(app_icon)
I have got a task bar icon in Windows 7 and correct icons in all windows without any changes to ui files.
You need to call setWindowIcon(...) on the window, not on the application.
Here's an example, which works for me:
#!/usr/bin/env python3
import os
import sys
import subprocess
import os.path
from PyQt4 import QtGui
from PyQt4 import QtCore
class MyWin(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__(parent)
self.setWindowTitle("My Window")
self.setWindowIcon(QtGui.QIcon('test_icon.png'))
self.show()
def main(args):
app = QtGui.QApplication([])
ww= MyWin()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv[1:])
For me, the following code works for both changing task bar icon and window icon
win.setWindowIcon(QIcon('logo.png'))
Experimenting with a battery monitor icon at the moment in Python using pygtk and egg.trayicon to create an icon to display a battery icon/tooltip.
I seem to be able to add the icon and the tooltip text, but when it then reaches the gtk.main() stage I need a way to modify these so it can then show the updated values.
I've tried gobject.idle_add() and gobject.timeout_add() without much luck, not sure where to go from this.
Anyone got any ideas?
EDIT: Perhaps not the clearest of questions.
I need to loop, fetching information from acpi while running and apply it to widgets inside the gtk container.
EDIT 2: Ok, it's properly down now. The issue was that I wasn't returning anything inside my callback. I just gave it "return 123" and now it's happily chugging away in my system tray, notifying me of my battery percentage :)
This example works for me:
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
import gobject
import gtk
from egg import trayicon
label = gtk.Label("Over here")
def callback(widget, ev):
label.set_text("You found me")
def timeout():
label.set_text("What are you waiting for?")
tray = trayicon.TrayIcon("TrayIcon")
box = gtk.EventBox()
box.add(label)
tray.add(box)
tray.show_all()
box.connect("button-press-event", callback)
gobject.timeout_add(3000L, timeout)
gtk.main()
Without seeing your code, it's hard to tell what doesn't work.