my tkinter gui is invisible for unknown reasons - python

I've been trying to create a basic gui using tkinter, I've done it before on a different computer but for some reason its invisible. Is there something wrong with my code or the computer (windows)?
import sys
from tkinter import *
mygui = Tk()
mygui.geometry('300x300+0+982')
mygui.title("my gui")
mygui.mainloop()

Is your screen height is bigger than 982 pixel?
Following line place the window at (0, 982) with width 300, height 300. If your screen height smaller than 982 pixel, you can't see it.
mygui.geometry('300x300+0+982')
Replace it with following:
mygui.geometry('300x300+0+0')
and you will see it.

Related

using the turtle module without opening the canvas (to obtain coordinates)

Is it possible to use the turtle module to obtain coordinates (turtle.pos() once at the desired locations) without it opening a canvas?
I know this probably seems silly and defeats the point of the turtle module, but it's actually a very useful way of getting the coordinates of some points I need.
Many thanks
Instead of running turtle standalone, you could run it embedded and withdraw the root window on startup:
import tkinter as tk
from turtle import RawTurtle
root = tk.Tk()
root.withdraw()
canvas = tk.Canvas(width=500, height=500)
turtle = RawTurtle(canvas)
turtle.sety(-1)
turtle.circle(1, extent=45)
print(turtle.position())

the window size is bigger than the specific size when create a window with a fix size

from tkinter import *
master = Tk()
master.resizable(False, False)
master.geometry("100x100")
master.mainloop()
When using the simple code above to create a fix size window, the actual window client area's size is about 126x126 pixels. How to create a window with exact size in tkinter in python?

tkinter window not wide enough to display window title

Is there a 'standard' way of ensuring that the displayed window is wide enough to display the window title?
import tkinter as tk
root = tk.Tk()
root_f = tk.Frame(root)
root.title('Long Window Title Containing Much Text')
text_f = tk.Frame(root_f)
text_l = tk.Label(text_f, text='Short text')
root_f.grid()
text_f.grid()
text_l.grid()
root.geometry('+{}+{}'.format(100, 100))
root.mainloop()
root.quit()
When I use the winfo method to get the width of the root (or text) frames they give the size of the text not the size of a window wide enough to display the whole window title.
I know it has to be something simple, but I can't see it.
Thanks
No, there is no standard way. Tkinter has no way of knowing how long the title is on the titlebar. The OS / window manager has completely control of that portion of the window and doesn't expose any platform-independent way of getting at that information.
You would have to know not only the font used by the OS for window titles, but also whether there were any other decorations (eg: buttons, images, etc) that appear in the title area.
If you're fine with making assumptions that the font is the same as the default tkinter font, you can use tkinter's ability to measure the length of a string in a given font with the measure method of a font object.

Stopping screen resolution changing layout of python game

I'm making my program on a 1200 by 800 screen, but when I tried to run it at school, which is (I think) 1280 by 1024, the format changes a lot and gets all messed up. In python, is there a way to stop a different sized screen from changing how the program looks?
I am using tkinter and have tk.resizable(0, 0) which stops the user from pulling the bottom right corner and changing the size of the window - However, tk.resizable only stops the user from changing the size of the window.
My code for drawing the canvas is:
global tk
tk = Tk()
VarExt = 0
tk.title("Pong Revolution - Version 9.2.8")#Title at top of window
tk.resizable(0, 0) #stops window being resizable
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=700, height=700, bg='black',)

Using Tkinter, is there a way to get the usable screen size without visibly zooming a window?

You can get the usable screen size (screen minus task bar, regardless of where it sits) like this:
import Tkinter as tk
root = tk.Tk()
root.state('zoomed')
root.update()
usable_width = root.winfo_width()
usable_height = root.winfo_height()
Is there a way to do it that is not visible to the user? In Tkinter, 'withdrawn' (hidden) and 'zoomed' are mutually exclusive states.
You can get the total screen size by adding:
total_width = root.winfo_screenwidth()
total_height = root.winfo_screenheight()
So far I've been unable to find a way to do this. Any ideas?
'zoomed' did not work for me in Linux Mint, but I finally found something that did and I think it is more portable. This can even be called while the window is withdrawn so that the user does not see the changes.
w, h = root.maxsize()
You can call:
root.attributes("-alpha", 0)
after creating the window. This will make it invisible, and then you can perform size calculations in the same way you already are.

Categories

Resources