I want to set a window's size, and then be able to resize it while the program is running. I've been able to make the window large, but I can't resize it smaller than the original set size. For a different project, I would also like to know how to make it so the window is not resizable at all.
For the first question: Gtk.Window.resize(width, height) should work. If you use set_size_request(width, height), you cannot resize your window smaller than these values.
For the second question: Gtk.Window.set_resizable(False)
Related
Hi I made the GUI of this program in 1980x1080 and everything is fit to the window, but when I run the program on the other pc of resolution 1366x768 3/4 of the GUI is outside the display.
This is my constructor for the GUI
class Main:
def __init__(self,master = None):
self.master = master
self.master.geometry(str(master.winfo_screenwidth())+'x' +str(master.winfo_screenheight()))
self.master.state('zoomed')
self.variables_for_graphs()
self.variables_for_inputer()
self.variables_for_graphs_menu()
self.place_frames()
screenshot of the program in 1980x1080 display resolution:
screenshot of the program in 1366x768 display resolution:
I add scaling to the constructor
dpi = master.winfo_fpixels('1i')
factor = dpi / 72
master.tk.call('tk', 'scaling', factor)
it did not help
if anything else is need please comment
you can multiply every dimension in your GUI by the ratio between the current resolution and the original resolution, but that would result in very ugly look, instead of manually setting the sizes of each object you should let tkinter size your objects.
for grid layout (similar to yours) you have. Tk Geometry manager
widget1.grid(row=0,column=0,rowspan=2,sticky="NWSE")
widget2.grid(row=0,column=1,sticky="NWSE")
widget3.grid(row=1,column=1,sticky="NWSE")
parent_widget.rowconfigure((0,1),weight=1,minsize=200)
parent_widget.columnconfigure((0,1),weight=1,minsize=200)
to set your widgets to grow to fill all its master space, with a minimum size of 400x400.
for widgets that you pack you should use
widget.pack(expand=True,fill="both")
which will also make the child grow to fill its parent space, the size system may look intimidating at first, but once you start using it you can get any shape you want with it that scales the way you want.
avoid using place as you have to manually manage its size (unless you have an object that must be of fixed size and position like a logo or floating widget), and avoid filling numbers for sizes yourself, things that you can fill yourself are probably padding, buttons size (sometimes), and font size (under different dpi), you should have tkinter manage other dimensions in your GUI otherwise your GUI won't scale well.
How can I get the size of the actual display, without using event.dict['size']?
Suppose I have a game with two screens, 'Menu' and 'Game', both of them resizable.
To make all images scale, I use in the beginning in the main file:
window = pygame.display.set_mode(reso,pygame.RESIZABLE)
window_fake = window.copy()
and inside the loops of the two screens I blit all images on the fake window and blit that to scale on the display window:
window.blit(pygame.transform.scale(window_fake, reso_act),(0,0))
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.VIDEORESIZE:
reso_act = event.dict['size']
Now all images scale along when resizing the display. However, when I go from 'Menu' to 'Game' after resizing it, I need the size of the actual display to use as my variable reso_act, because I again blit everything onto my fake window and scale that to the display window, however there is no VIDEORESIZEevent.
So now if I make the display smaller in 'Menu', then click start and draw 'Game', it will have the original resolution and half of the images are outside of the display.
I have tried the following, but they all give me the value of reso, the original unresized resolution:
window.get_size()
window_fake.get_size()
pygame.display.get_surface().get_size()
window.get_rect()
window_fake.get_rect()
Is there a way to get the real size of the display?
Or otherwise a smarter way to achieve what I want to do?
I now solved it by returning reso_act every time from all the functions for the different screens and also passing it as input for all those functions, so it remembers the setting.
It is not so elegant, but it works.
If someone knows how to get the actual size of the display I would still be interested to know though.
I am making a program where pictures are displayed to the user. Some of these pictures are, however, too long and run off the canvas. The canvas is a set size(width=600, height=150). I am wondering if there is anything to make sure the longer pictures do not exceed this width and are shrunk down when they do so they fit.
The images are in .gif format.
I have tried using subsample() and zoom() but these seem to shrink the images even when they already fit on the canvas making them too small.
subsample and zoom are the only way to resize images with the tkinter PhotoImage objects. Whey you call them, they will always shrink or grow the image. It is up to you to determine which one to call, and what arguments that will give you the closest approximation to the desired size.
Someone know if it's possible to change the color of a pixel in a canvas without using un object, so without using something like canvas.create_oval or canvas.create_rectangle ?
There is no way to color a pixel other than to create a 1x1 pixel object of some sort. And yes, at some point you will experience performance problems. The canvas simply wasn't designed to be used this way.
If you're really needing to create a large area in which you can manage individual pixels, you can create a canvas with a single image that is the same size as the canvas. You can then set the color of individual pixels on the image through the photo image interface.
Within tkinter itself, it's impossible.
Even if you manage to change a pixel on canvas window (which is possible with X11 and Windows APIs in a platform-dependent way), you'd have to ensure it's repainted properly.
You can, of course, place a frame of size 1x1 over the canvas, with a background color you want. This way, pixel is "changed" and no canvas object is created. If there's a real (though strange) problem behind a question, this trick could be a solution.
Ideally, the transparent border.
Here's an example of what i'd like to achieve:
Notice the transparent border.
Now i suppose I could use cairo to create a rectangle with transparency, and put a borderless non-transparent window inside, mimic'ing that effect - which I would if i knew the window would have a fixed dimension. However, if the inner window grows, it'll grow out of the transparent rectangle.
How should one approach such task?
Making window frames is really the job of the window manager (at least under X11, don't know how it works on windows).
But have a look at the GtkBin, GtkBox or GtkMisc widgets. Pack the dialog inside it as a single widget, and use padding to give it a size. Read up on GTK+ drawing model. You will probably need to set a flag and define your own expose-event handler to re-draw your frame.