Weird white border top left of whole tkinter application - python

This is the first tkinter application I've ever made and I'm sure there are other issues with it but it works well for me. The only issue I have is for some reason on my 4k monitor there is a little white border on the top left side of the application. This does not appear on my 1080p monitor. Edit: It appears this is due to adding an icon. When I have an icon it happens. When I remove the icon part it goes away. This obviously isn't a solution though since I still want an icon for my application. I'll leave a picture of the issue and the code for the application here.
I'm not allowed to post images yet so here is a link for it
from tkinter import *
#Main Section
app = Tk()
app.title('Rust Furnace Calculator')
app.iconbitmap('Rust.ico')
app.configure(background='#33393B')
app.resizable(False, False)
app.geometry("500x500")
#Program Loop
app.mainloop()

Related

Is there a function in tkinter to figure out the screen orientation

Trying to make an app for Android as my school project I'm using tkinter and wanted to know if there's a function that can just tell you the screens orientation
I tried using a loop to compare the screens height and width and then determine the orientation that way but the window didn't appear because of the loop. Pls help kinda need to submit in 2 weeks. I'm also okay with a way to make it so that the above mentioned loop doesn't stop the window appearing
If your end user is using a single monitor, you can probably get away with something like this
root = tk.Tk()
def get_screen_orientation():
width, height = root.winfo_screenwidth(), root.winfo_screenheight()
if width >= height:
return 'Landscape'
return 'Portrait'
It gets trickier on multi-monitor setups, however, because only the dimensions of the primary monitor will be shown.
EDIT: I am not aware of any way to create a mobile app using tkinter (at time of writing). However, I'm gonna leave this answer up because it is still viable in a typical tkinter app. You have been warned.

Python/Tkinter: erratic behaviour using root.overridedirect(True)

The following code disables the tkinter title bar, creates a replacement title bar, and enables the user to re-position the app on the screen by selecting and holding the replacement title bar and moving the mouse.
import tkinter as tk
root = tk.Tk()
root.geometry("1275x675+50+70")
root.overrideredirect(True) # remove tkinter title bar
def movewindow(event): # event bound to new title bar
root.geometry(f'+{event.x_root}+{event.y_root}')
titlebar = tk.Frame(root,borderwidth = 0, bg="lightblue",
width=1275,height=30)
titlebar.place(x=0,y=0)
titlebar.bind("<B1-Motion>",movewindow)
root.mainloop()
The code works perfectly in my main monitor. But if I move the app to my second monitor, it will disappear. Strangely, if I click anywhere on the second monitor it will re-appear, and after that, the app can be moved seamlessly across both monitors.
I should note that, when I click on the titlebar and begin to move the app, the cursor jumps to the left, so that I'm moving the app with the mouse pointer at the left hand corner of the title bar. The trouble begins the moment the mouse pointer leaves the main monitor.
If I initialize the root geometry on startup to +1600 instead of +50, the app will launch in the second monitor. And it can then be moved seamlessly between both monitors. Unfortunately, I want the app to launch in the main monitor, so that is not a workable fix.
Is there any fix that could be applied here?

how to change a window border color in tkinter

I'm having a problem with changing the background of this part of the window:
How do I change it?
As I see you are using windows.
This color is set by the theme you are currently using. It is the same for every window.
So I cross out the possibility of only using the Tkinter module for this.
Tkinter is responsible for what is in the window but the window manager decides about the border. For example in Ubuntu the window would look totally different.
I guess, you would need some windows specific calls for that.
You can remove the border with root.overrideredirect(1) if I remember correctly.
PS: put "windows" into the tags of this question.

How to create wxpython app that attach to top and shift desktop size, like Taskbar?

I am using WxPython (phoenix), and at the moment trying to create Taskbar replacement in Windows 10, always on top, and at the top edge of the screen. Surely, it will make my app cover window beneath it.
How do I make the desktop shrink to allow my app attach itself to the top edge of the screen?
Woops, sorry, doing a quick google, I found this: https://github.com/sabren/ceomatic/blob/master/wxappbars.py
Only need to change for has_key error and GetMetrics to wx.SystemSettings.GetMetric per Phoenix changes

Python capture events when console has focus with Tkinter

I'm building a console app, and would like to capture keystrokes in real time. The following code works perfectly until another window gets focus. From that point on, I'm not able to get back to a state where I can capture keystrokes and other events again with only the console visible.
import tkinter as tk
app = tk.Tk()
def handleKeypress(event):
key = event.char
if(key == 'q'):
app.destroy()
else:
print(key)
app.bind_all('<Key>', handleKeypress)
app.withdraw()
app.mainloop()
I've tried using various methods (grab and focus) to redirect the focus to my app. The best I was able to do was to get the Tkinter window visible and in focus with deiconify(), but I was not able to hide it again to make it as though the console is the only window.
Adding the following results in the Tkinter window appearing and disappearing repeatedly:
def lostFocus(event):
app.deiconify()
app.focus_force()
app.withdraw()
app.bind_all('<FocusOut>', lostFocus)
How can I go back to the state the application was in right after launch? Or even better, how can I force it to get all events without having to make the Tkinter window visible and in focus?
You can't do what you want. Tkinter is designed -- as are most GUI toolkits -- to only process events when it has the focus. That's the whole point of focus: for the OS to know where to send events.
The fact that it works initially is probably a bug in tkinter. Though, perhaps it can be explained by the fact that the window initially has focus, and when you withdraw the window the OS doesn't move the focus
The only way to restore focus is to make the window visible.

Categories