it seems that I was pending to continue the bombardment of questions. It this is short, Is it possible to disable the movement of the Tkinter window without deleting the top bar of this?
It would give a minimal and reproducible code, but if it did it would only be two lines, it would be useless.
Bind a event for your window,and set the window .geometry()
But now you can not revise the window size by dragging the window's border(But it can maximize the window.).
Here is an example of the code:
import tkinter
def GetWindowPos():
global X,Y
X = win.winfo_geometry().split("+")[1]
Y = win.winfo_geometry().split("+")[2]
win.bind_all('<Configure>', HoldOn)
def HoldOn(event):
win.geometry("+{}+{}".format(X,Y))
win = tkinter.Tk()
win.geometry("400x400+{}+{}".format(12,12))
tkinter.Label(win,text="Halo!").grid()
win.after(100,GetWindowPos)
win.mainloop()
I have found a method, but as you might know to achieve something, we have to lose something!
You can use:
root.overrideredirect(True) # turns off title bar
by which you wont be able to move the tkinter window and also Tkinter application won't be displayed in taskbar, but you will also lose the title bar.
but if you wish to have the title bar,
then you can create one by this link.
Or use below to make a new title bar and also be able to move it(from this answer)
def move_window(event):
root.geometry('+{0}+{1}'.format(event.x_root, event.y_root)
# bind title bar motion to the move window function
title_bar.bind('<B1-Motion>', move_window)
But still your Tkinter application won't show up in taskbar, here's a solution(from this answer):
root = tkinter.Tk()
top = tkinter.Toplevel(root)
root.attributes("-alpha",0.0) # to make root invisible
#toplevel follows root taskbar events (minimize, restore)
def onRootIconify(event): top.withdraw()
root.bind("<Unmap>", onRootIconify)
def onRootDeiconify(event): top.deiconify()
root.bind("<Map>", onRootDeiconify)
You can add a toplevel window under the root object, make toplevel invisible and then handle the icon events of top level to hide or show the root window on taskbar.
Related
Hey guys so I am trying to make a timer app for studying which pops-up when break time is triggered. I am using this code to bring the window up, but the issue I am facing is if I minimize the window this code does not work anymore and the app does not pop-up. I tried to find a way to just remove the minimize button option from the window but didn't come across anything.
def raise_above_all(window):
window.attributes('-topmost', 1)
window.attributes('-topmost', 0)
You can use window.deiconify() to make it un-minimize, and use your code to make it go to front. Like this:
window.deiconify()
window.attributes('-topmost', 1)
window.attributes('-topmost', 0)
Don't go for disabling the window's minimize button. Just use window.state('zoomed') along with window.attributes('-topmost', True). This way even if the window was minimized, still it will be raised to the topmost level.
Also, define the window's maxsize prior, so that the window does not become fullscreen due to zoomed.
Here is a simple yet complete demonstration:
(To raise the window "window2", you would need to Enter 1 in the entry box of "window1")
from tkinter import *
window1 = Tk()
window1.geometry('1200x600')
window1.minsize(1200, 600)
def raise_above_all(window):
window.state('zoomed') #Needed in case the window2 is minimized
window.attributes('-topmost', True)
def create_new_window2(window):
window.maxsize(300, 300) #define the maxsize so that the window2 does not get fullscreen when 'zoomed'
window.mainloop()
def submit():
if x.get()==1:
raise_above_all(window2)
else:
print("Enter 1")
x = IntVar()
Entry(window1, textvariable=x).pack()
Button(window1, text="Enter", command=submit).pack()
window2 = Tk()
create_new_window2(window2)
window1.mainloop()
Alternatively, you can just remove the entire window's title bar using window.overrideredirect(True)
I have this code where I click the button and tkinter window shows an image along with a tk root window.
I tried using root.withdraw() however this just ends up creating the tkinter root window and does not display my image.. Would need help in showing the image only and hiding the tkinter root window as well
def on_click_button():
global stage
stage == 'Menu'
root = tk.Toplevel()
photoImageObj = tk.PhotoImage(file="showimg.png")
lab = tk.Label(root, image=photoImageObj).pack()
root.withdraw()
root.mainloop()
print('You clicked samsung note 20')
Tkinter sets the master attribute of a widget to be a reference to the master for that widget. Since you didn't explicitly set the master for the instance of Toplevel, master will be the root window. So, in this specific case you can call withdraw on root.master:
root.master.withdraw()
Personally, I would recommend against naming your Toplevel "root" since it's not the true root window. It makes the code a bit harder to understand since a true root window will not have a master.
You will want to provide a mechanism for showing the root window at some point, since withdrawing it will keep it alive but not visible.
I'm new using tkinter on python and I would like to develop a program that can Drag and Drop a button pressing other one... I will try to explain : I have button 'A' that will create a new button 'B' and I want to Drag the New button to another place
Any help
Thanks
The tkinter.dnd module, as suggested by j_4321 in comments.
Here is some sample code using that library to do what you have said:
from tkinter import *
from tkinter.dnd import Tester as DragWindow, Icon as Dragable
# Make a root window and hide it, since we don't need it.
root = Tk()
root.withdraw()
# Make the actual main window, which can have dragable objects on.
main = DragWindow(root)
def make_btn():
"""Make a new test button."""
# The functional part of the main window is the canvas.
Dragable('B').attach(main.canvas)
# Make a button and bind it to our button creating function.
Button(main.top, text='A', command=make_btn).pack()
# Start the mainloop.
mainloop()
I am working on a project using Tkinter library for making a GUI. This GUI will be displayed on a touch screen using raspberry pi 3.
I want to prevent user from exiting or minimising the program.
Is there any way to disable or remove the title bar? Or is there a better way to achieve this?
Since you mentioned a raspberry pi I suppose you are using Linux. In this case you can use root.attributes('-type', 'dock') (assuming your Tk instance is called root). This way, your window will have no decoration (so no close or minimize buttons) and will be always on top. If you don't want it always on top, you can use type 'splash' instead. In any case, you will need to use focus_force to be able to get keyboard focus.
import tkinter as tk
root = tk.Tk()
root.attributes('-type', 'dock')
root.geometry('200x200')
tk.Entry(root).pack()
root.focus_force()
root.mainloop()
Otherwise, you can prevent the window from being closed by setting the 'WM_DELETE_WINDOW' protocol and redisplay the window each time it is minimized:
import tkinter as tk
root = tk.Tk()
def unmap(event):
if event.widget is root:
root.deiconify()
root.protocol('WM_DELETE_WINDOW', lambda: None) # prevent closing
root.bind('<Unmap>', unmap) # redisplay window when it's minimized
root.mainloop()
root = tk.Tk()
root.wm_attributes('-type', 'splash')
For more details go to this link: Remove titlebar without overrideredirect() using Tkinter?
I have a toplevel window that keeps appear underneath the root window, which is pretty annoying. Any idea how to make sure that the toplevel gets the focus when the function is called?
def setup(self):
self.setup_step = 1
setup_window = Toplevel(self)
setup_window.config(bg = "#000000", height = "600", width = "850")
setup_window.title("RoastMaster Setup")
setup_window.geometry("800x600")
You can try by making the toplevel window transient.
You might try raising it and giving it focus. It could be that you window manager wants the window with focus to always be on top.