I've been wondering if there were other types of window-oriented modules than tkinter (too simple in my sense) or pygame (too much game-oriented). For example, if it were possible to display things with coordinates and not Up/Down like tkinter it would be better I guess.
Asking for suggestions is not a good fit for Stack Overflow but your other question about grid layouts is a good question!
Grid Layout with tkinter
Tkinter, like other desktop window managers have things called layout managers. Layout managers provide a format for displaying/ organising components.
Using this reference i have prepared a short summary of the tkinter grid layout
from Tkinter import *
colours = ['red','green','orange','white','yellow','blue']
r = 0
#generate a set of components and place them in specific cells with the .grid() call
for c in colours:
Label(text=c, relief=RIDGE,width=15).grid(row=r,column=0)
Entry(bg=c, relief=SUNKEN,width=10).grid(row=r,column=1)
r = r + 1
#run application
mainloop()
Produces:
Related
How do I make a Tkinter GUI have rounded corners?
Example:
If you mean the window itself, I am afraid that is not possible due to the fact that Tkinter needs to be supported by 3 different operating systems, all with different window handlers. If you wish to attempt to do this, you will need to find a OS-specific library to do this. If you mean having a canvas with a rounded rectangle inside it, this question, which jezza_99 mentioned, covers this quite nicely.
im doing an tkinter app in a computer, im using the grid() method to place the widgets. At first of the program i use this code to make the window size like the screen size:
an = self.root.winfo_screenwidth()
al = self.root.winfo_screenheight()
self.tam = '%dx%d'%(an,al)
self.root.geometry(self.tam)
And it works, but this app will be used through a remote desktop with different devices (different screen sizes).
How can I do that the widgets fill on the window like the original design? Thanks
Without any concrete examples of your code, there's no way to give more specific advice than to say that the solution is to design your program so that it resizes well.
Tkinter excels at making widgets fit, so as long as you use the options at your disposal (fill and expand for pack, row and column weights and other options for grid), and you don't hard-code any widths and heights, your GUI will easily work on a variety of systems.
Concrete pieces of advice:
don't use place except in very rare circumstances. While place supports relative positioning and sizing, it requires more work than pack and grid
design the GUI to work on the smallest display possible, and then make sure that when you manually resize the window it behaves properly
When using grid, make sure you always have at least one row and one column with a non-zero weight so that it knows how to allocate extra space
When using pack make sure you use expand and fill properly
Don't turn off the ability for the user to resize the window
I'm trying to include an interactive vpython window in a tkinter frame or canvas in order to visualise simple shapes (representing the mechanical assembly that will be sized). I can't find a frame or canvas method to integrate a vpython window properly. Do you know if it's even possible ? If it's not doable, which programming language should I pick to realize a GUI including 3D visualisation ?
Thanks for your help!
Searching for the best way to clear the Tkinter icon, I found Removing the TK icon on a Tkinter window. But all the answers given there seemed unsatisfactory to me: I wished to do it in a platform-independent way, without additional files and without (compressed) inline data.
However, the solution I found – and that's why I didn't post this a an answer – has a small flaw that I would like to understand.
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
photo = ImageTk.PhotoImage(Image.new('RGBA', (1,1), (0,0,0,1)))
root.iconphoto(False, photo)
root.mainloop()
The alpha value I'm using here is 1 instead of 0, so there is no full transparency even if probably no one will be able to see the difference. If I use 0 to get full transparency as documented, a small black square (larger than 1x1) is shown instead.
I wasn't able to find any information about what causes this strange (but maybe acceptable?) behavior. And so I would be happy if any of you could give me an explanation.
I want to put a Canvas with an image in my window, and then I want to pack widgets on top of it, so the Canvas acts as a background.
Is it possible to have two states for the pack manager: one for one set of widgets and another for another set?
The answer to your specific question is no. You can't have two states or otherwise use pack two different ways in the same parent.
However, what I think you want to accomplish is simple. Use the built-in features of the canvas to create an image item that is part of the canvas, then pack things into the canvas as if it were a frame.
You can accomplish a similar thing by creating a label widget with an image, then pack your other widgets into the label.
One advantage to using a canvas is you can easily tile an image to fill the whole canvas with a repeating background image so as the window grows the image will continue to fill the window (of course you can just use a sufficiently large original image...)
I believe that Bryan's answer is probably the best general solution. However, you may also want to look at the place geometry manager. The place geometry manager lets you specify the exact size and position of the widget... which can get tedious quickly, but will get the job done.
... turned out to be unworkable because I wanted to add labels and more canvases to it, but I can't find any way to make their backgrounds transparent
If it is acceptable to load an additional extension, take a look at Tkzinc. From the web site,
Tkzinc (historically called Zinc) widget is very similar to the Tk Canvas in that they both support structured graphics. Like the Canvas, Tkzinc implements items used to display graphical entities. Those items can be manipulated and bindings can be associated with them to implement interaction behaviors. But unlike the Canvas, Tkzinc can structure the items in a hierarchy, has support for scaling and rotation, clipping can be set for sub-trees of the item hierarchy, supports muti-contour curves. It also provides advanced rendering with the help of OpenGL, such as color gradient, antialiasing, transparencies and a triangles item.
I'm currently using it on a tcl project and am quite pleased with the results. Extensions for tcl, perl, and python are available.
Not without swapping widget trees in and out, which I don't think can be done cleanly with Tk. Other toolkits can do this a little more elegantly.
COM/VB/MFC can do this with an ActiveX control - you can hide/show multiple ActiveX controls in the same region. Any of the containers will let you do this by changing the child around. If you're doing a windows-specific program you may be able to accomplish it this way.
QT will also let you do this in a similar manner.
GTK is slightly harder.