I am trying to change the background color of the ttk Notebook widget in my python tkinter application so that it matches the blue color instead of being white.
I've tried searching for hours but to no avail. Is it not working because I'm working on a mac?
tabs_notebook = ttk.Notebook(self)
tabs_notebook.configure(bg=DARKBLUE)
tabs_notebook.pack()
view_tab = ttk.Frame(tabs_notebook)
new_tab = ttk.Frame(tabs_notebook)
tabs_notebook.add(view_tab, text="View Patient Details")
tabs_notebook.add(new_tab, text="Add New Patient")
One of the major complaints levelled at Tk was that it was ugly. This was mostly because it never fit the native look and feel of the hosting environment and because the amount of configuration options provided meant application developers tended to roll their own style which never fit well with any other applications. The other problem was that on Windows and MacOS Tk used naive rendering APIs to draw buttons and other UI elements but failed to keep up when these platforms introduced theming. As a result even if the Tk application looked ok on default Windows XP, it would look out of place where a user selected an alternate theme.
So in developing ttk (themed Tk) one of the primary design goals was to ensure that the default theme fit the current platform. On Windows and MacOS this means that UI elements are drawn using the native theming APIs. On Linux - it just has to make a best guess.
In your specific case the notebook elements are being drawn using MacOS themeing API calls and you cannot change the colors for this theme from Tk. However, what you can do is define an entire Tk theme. There are some examples in Tk code doing this and these were imported to Python by someone. Those should yield enough to generate a new theme using Tk drawing APIs and possibly images for some elements to allow for a fully custom theme should you require that amount of customization.
Related
I have made a gui using Tkinter and when i send that code to my friend to further work on, the places of buttons and other widgets get distorted.Is there a way by which i can assure that whatever OS he is using, it will show the same result as it shows on my system.
My friend is using mac and i'm working on windows.
My application looks beautiful on Windows
But on MacOS and Linux it does not look nice at all!
Is there something I can do to help make this widget appearance consistent across operating systems?
My application source code : http://pae.st/BILs/
Using Tkinter's place manager for widget layout causes the poor layout of your application on different operating systems. You should be using either pack or grid managers, both of which will adjust to the differing widget sizes you encounter.
All windows made through Perl TK or Python Tkinter look like default Windows-styled window, with red cancel button on top right, preceded by maximize and minimize buttons, blue top bar, etc. Is it possible to make custom windows, like those we see for downloaded softwares, where everything, right from the color, to position of buttons, their styles, etc are customized?
You can turn off the standard decorations in a few ways, e.g., by setting the toolwindow boolean attribute (Windows only), by making it an overrideredirect window, or (with a new-enough Tk) by setting the type attribute of the window to something like utility (X11 only). With the standard decorations disabled, you can then draw anything you want (which is how the other programs you mention do things), though there are a number of restrictions, particularly with focus handling. Override-redirected windows often don't participate in the keyboard management regime, because they're mostly invisible to the window manager which doesn't know to direct focus to them in the first place. (IIRC, you can force it but then you're getting into a fight with the WM and that's difficult to get right; “don't fight the WM” is one of the good rules of thumb for GUI design.) You can also set the window as transient (i.e., working for another window) which often reduces decoration levels.
The way you set these things depends on the language you're using. I can point to the places to look in the “mothership” documentation, but how they work in different languages does vary.
Python 2.7 (32-bit) Windows: We're experimenting with Python 2.7's support for themed Tkinter (ttk) for simple GUI's and have come away very impressed!! The one area where the new theme support seems to have come up short is how OS specific common dialogs are wrapped.
Corrected: In other words, the MessageBox and ColorChooser common dialogs have "ugly" looking Win 95 style blocky looking buttons vs. the themed (rounded/gradient) buttons that normally show up on these common dialogs under XP, Vista, and Windows 7. (I'm testing on all 3 platforms with identical, un-themed results).
Note: The filedialog common dialogs (askopenfilename, askopenfilenames, asksaveasfilename, askdirectory) are all properly themed.
import tkMessageBox as messagebox
messagebox.showinfo()
import tkColorChooser as colorchooser
color = colorchooser.askcolor( parent=root, title='Customize colors' )
Any ideas on what's required to get Tkinter's MessageBox and ColorChooser common dialogs to be OS theme compatible (at least under Windows XP or higher)?
Your observation is mainly correct. I do see what you are referring to in the messagebox and the colorchooser. However, my filedialogs all seem to have properly rounded buttons, etc.
My recommendation for you on making the messagebox is to create your own messagebox using the TopLevel widget, and then define what you need on it and the appropriate behavior for the different buttons (it's definitely a bit harder than just using a messagebox, but if you really need the new style buttons, it'll work).
I don't think you can hack together a solution for the colorchooser problem, however.
I though for a minute that perhaps Python 3.1 had fixed this problem, but sadly, I tried and that isn't the case. I suppose if you need the user to pick a color, the buttons will have to be ugly.
An option to get better looking dialog boxes is to compile your script to an executable using pyinstaller. I explain this more thouroughly here.
tl;dr, it appears that compiling with pyinstaller allows you to have dialog boxes with the style of the currently running OS, but not custom styles.
It's likely that this is just a general Python Tkinter question, not necessarily a matplotlib one.
So I'm in the midst of developing a rather large suite of plotting functionality on top of matplotlib using the Matplotlib "TkAgg" backend (Agg rendering to a Tk canvas using TkInter). I'm using some of the default zooming functionality provided by matplotlib out of the box...specifically the "Zoom to box" button on the default matplotlib toolbar. I am creating my own toolbar by subclassing the existing "matplotlib.backends.backend_tkagg.NavigationToolbar2TkAgg" class.
Pretty much, the issue here is that I hate the default icon that "Zoom to box" uses (the Tkinter "tcross"). I've figured out how to use a different Tkinter built-in cursor (e.g. this changes the cursor to "plus" instead of "tcross"):
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.backend_bases
import matplotlib.backends.backend_tk_agg
matplotlib.backends.backend_tkagg.cursord[matplotlib.backend_bases.cursors.SELECT_REGION] = "plus"
And in general, I know that to change the current mouse cursor to one of the built-in Tkinter ones from the toolbar class, I can just call:
self.window.configure(cursor="cursor_name")
So what I would really, really like is to be able to use a magnifying glass icon for when the user is in "zoom mode". I already have a .ppm of the magnifying glass icon I'd like to use and everything, but I can't figure out for the life of me how to use my magnifying glass as the mouse cursor icon. Is it possible to use a custom image as a mouse cursor in Python Tkinter? Help!
Platform note: This needs to be workable on Mac OS X 10.5+, RedHat Enterprise Linux 5, and possibly Solaris 10, so a platform-specific solution is undesirable.
Something like this works with unix X11 XBM files:
import Tkinter
t = Tkinter.Tk()
t.configure(cursor=('#/usr/include/X11/bitmaps/star', '/usr/include/X11/bitmaps/starMask', 'black', 'white'))
t.mainloop()
As for the Macs, from the man page for "Tk_GetCursorFromData":
The Macintosh version of Tk supports all of the X cursors
and
will also accept any of the standard Mac cursors
including
ibeam, crosshair, watch, plus, and arrow. In addition, Tk
will
load Macintosh cursor resources of the types crsr (color)
and
CURS (black and white) by the name of the of the resource.
The
application and all its open dynamic library's resource
files
will be searched for the named cursor. If there are
conflicts
color cursors will always be loaded in preference to
black and
white cursors.