i am working on a tkinter app and I just updated to the latest version of python(python 3.9.5) and tkinter is using my mac's default theme, which doesn't go with my design.
i want this(the light theme):
but what am getting is(the dark theme):
is there a way I can change the theme to light or to check the current mac theme?
my code:
from tkinter import *
class Snipper:
def __init__(self):
self.root = Tk()
self.root.title("snipper")
self.build()
# bring app to front after start
self.root.lift()
self.root.attributes('-topmost',True)
self.root.after_idle(self.root.attributes,'-topmost',False)
self.root.minsize(1000, 600)
self.root.mainloop()
s = Snipper()
Related
I'm currently trying to resize a Tkinter window to a specific size. It is working great except when the window is maximized using the maximize button in the top toolbar. Here is a simple code that I've done for example.
When the window is maximized using the button, the print output is "normal" while it should be "zoomed". The window's size is then locked until the maximize button is pressed again.
I am working on Red Hat Enterprise Linux if it can help. Has anyone already had this problem before?
from tkinter import *
class Main:
def __init__(self, root):
self.root = root
Button(self.root, command=self.resize, text="Resize").pack()
def resize(self):
print(self.root.state())
self.root.attributes("-zoomed", False)
self.root.geometry("800x300")
root = Tk()
x = Main(root)
root.mainloop()
Why does the following test not display the menu on my Mac running Python 3.7 on Mojave?
Tried a shebang but that didn't help.
import tkinter as tk
def quit_app():
my_window.destroy()
my_window = tk.Tk()
my_menu = tk.Menu(my_window)
my_menu.add_command(label='Quit',command=quit_app)
my_window.config(menu=my_menu)
my_window.mainloop()
The menu displays in Windows 10 but not on the Mac. The tkinter window is blank on the Mac.
The menu displays in Windows 10 but not on the Mac
Yes, that is because MAC has a design philosophy that all devs for their platform have to conform with.
You cannot have a add_command as a top level menu item on mac, rather:
import tkinter as tk
def quit_app():
my_window.destroy()
my_window = tk.Tk()
my_menu = tk.Menu(my_window)
quit_menu= tk.Menu(my_menu, tearoff=0)
quit_menu.add_command(label='Quit App',command=quit_app)
my_menu.add_cascade(label="Quit", menu=quit_menu, underline=0)
my_window.config(menu=my_menu)
my_window.mainloop()
environment:
Ubuntu Gnome 16.04.3
GNOME Builder 3.26.3
The GNOME Builder generated project use GtkTemplate to interpret ui, But I change it to use Gtk.Builder to interpret. After modifying, it does not work.
And I can not see any error message.
class Application(Gtk.Application):
def __init__(self):
super().__init__(application_id='org.gnome.Ee',
flags = Gio.ApplicationFlags.FLAGS_NONE)
def do_startup(self):
print('do_startup')
Gtk.Application.do_startup(self)
builder = Gtk.Builder()
builder.add_from_resource("/org/gnome/Ee/window.ui")
print(builder)
self.builder = builder
def do_activate(self):
print("do_activate")
win = self.props.active_window
if not win:
win = self.builder.get_object("EeWindow")
print(win)
win.present() # not work, can not see the window
I have upload the demo code to Github: https://github.com/Honghe/gnome_builder_demo
Any help will be appreciated!
The application does not know about the window so it exits immediately. You need to call win.set_application(self) after you created an instance of the window.
I have been looking for a way to display a right-click popup menu on OSX.
So far all of my attempts have been unsuccessful. The same code will work fine on a Linux VM(Ubuntu).
For arguments sake I copied the code written in these two pages and tried to run them on my machine.
tkinter app adding a right click context menu?
http://effbot.org/zone/tkinter-popup-menu.htm
Neither have worked in the way I expect them to on OSX but they do when I run them on an Ubuntu VM.
The machine I am using is a Mac Mini4,1 running OSX 10.6.8.
Has anyone else experienced this and is there a viable workaround?
For odd historical reasons, the right button is button 2 on the Mac, but 3 on unix and windows.
Here is an example that works on my OSX box:
try:
# python 2.x
import Tkinter as tk
except ImportError:
# python 3.x
import tkinter as tk
class Example(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.popupMenu = tk.Menu(self, tearoff=0)
self.popupMenu.add_command(label="One", command=self.menu_one)
self.popupMenu.add_command(label="Two", command=self.menu_two)
self.popupMenu.add_command(label="Three", command=self.menu_three)
self.bind("<Button-2>", self.popup)
def menu_one(self):
print "one..."
def menu_two(self):
print "two..."
def menu_three(self):
print "three..."
def popup(self, event):
self.popupMenu.post(event.x_root, event.y_root)
if __name__ == "__main__":
root =tk.Tk()
frame = Example(root, width=200, height=200)
frame.pack(fill="both", expand=True)
root.mainloop()
Because of the MacOS's mouse button index is different from Windows or Linux .You can try this in your code:
MAC_OS = False
if sys.platform == 'darwin':
MAC_OS = True
if MAC_OS:
self.bind('<Button-2>', self.context_popup)
else:
self.bind('<Button-3>', self.context_popup)
Background
I am about to resolve another issue, which consists in reserving screen-space for a Qt Window on X11. For this i use PyQt4 and Python-Xlib.
Situation
The application i want to save screen space for is a frameless Qt Window, 25px high and screen-wide, it's a panel in fact. The simplified code looks like this:
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
# this module i made myself (see below):
import myXwindow
class QtPanel(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
# here i explicitely name the window:
self.setWindowTitle('QtPanel')
self.resize(QtGui.QDesktopWidget().screenGeometry().width(), 25)
self.move(0,0)
self.setWindowFlags(QtCore.Qt.Widget |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.WindowStaysOnTopHint)
self.setAttribute(QtCore.Qt.WA_X11NetWmWindowTypeDock)
def main():
app = QtGui.QApplication(sys.argv)
panel = QtPanel()
panel.show()
xwindow = myXwindow.Window(str(panel.windowTitle()))
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Now to reserve space for this QtPanel application, i need to call on xlib. Here below i will just show how i intend to 'grab' my window from X. The following module is imported in the code here above to get the Xwindow of my panel:
from Xlib.display import Display
from Xlib import X
class Window(object):
def __init__(self, title):
self._title = title
self._root = Display().screen().root
self._window = self.find_window()
def find_window(self):
my_window = None
display = Display()
windowIDs = self._root.get_full_property(
display.intern_atom('_NET_CLIENT_LIST'),
X.AnyPropertyType).value
print 'looking for windows:'
count = 0
for windowID in windowIDs:
count += 1
window = display.create_resource_object('window', windowID)
title = window.get_wm_name()
print 'window', count, ':', title
if self._title in title:
my_window = window
return my_window
Problem
Now when i run the QtPanel application, it should return a list of names of all the windows currently displayed by X. Despite having been named explicitly in the code (see here above) the QtPanel application either has no name (i.e. None or '') or is named 'x-nautilus-desktop', here is a sample of what was returned:
$ python ./Qtpanel.py
looking for windows:
window 1 : Bottom Expanded Edge Panel
window 2 : cairo-dock
window 3 : x-nautilus-desktop
window 4 : ReserveSpace - NetBeans IDE 6.9
window 5 : How to provide X11 with a wm_name for a Qt4 window? - Stack Overflow - Mozilla Firefox
Question
How can i 'name' my Qt Application or Qt toplevel window so it shows properly on X?
And/or: how can i identify my application in X other than with the application's name?
Although i don't quite know what the unnamed window is (and it seems related to launching the QtPanel application, the Xlib script is not able to find the window on display because it is queried before the event loop QApplication._exec(). A thread may be required to do that outside of the main loop.