Drag and Drop button tkinter python - python

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()

Related

Disable window move in Tkinter Python

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.

How to remove title bar in Tkinter program?

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?

Detect click on border of widget in tkinter 3

I am trying to make a kind of movable widget program in python with Tkinter, but I ran into a problem. I can't detect a click without interfering with the function of the widget that you click on. (example: Text or Button widget)
Here is an example:
import tkinter as tk
main = tk.Tk()
notes = tk.Text(main, height = 15, bd = 4)
notes.place(y = 10, x = 20)
notes.bind("<Button-1>", lambda event: print("hello"))
But if you try and click in the middle, it still works. Is there any way to make it only clickable on the border and not the widget itself?
You can prevent the default behavior by returning "break" from the event handler. For example:
def hello(event):
print("hello")
return "break"
...
notes.bind("<Button-1>", hello)
The same works for any widget. This prevents the default behavior (moving the cursor, clicking the button) from happening.
Another choice is to put each widget in a frame with a small border, and then put the binding on the frame.

How To Hide Tkinter python Gui

anyone know how to hide python GUI Tkinter,
I 've created keylogger, for GUI I used python module Tkinter , I want to add button called HIDE, so when user click it it will hide GUI , and when user press key like CTRL+E , it should unhide GUI....?
To hide the default root window you can use
root.withdraw()
and to make it visible again you can use
root.deiconify()
Python 3:
from tkinter import Tk
root = Tk()
root.withdraw()
If you want to "hide" the window (NOT destroy it) and bring it back, then the way to do that is set the alpha channel.
To hide the window, set the alpha channel to 0:
master.attributes('-alpha', 0)
to restore the window set alpha to 1.0:
master.attributes('-alpha', 1.0)
[EDIT - had the incorrect type/range for the original answer. Range is 0 to 1.0 and is a float]
To get rid of a GUI window I used the following in my code.
window.destroy()
and the following to bring it up again.
nameoffunction()
window.lift()

How to show a window that was hidden using "withdraw" method?

I would like to show a window after I called withdraw.
The following is my current code:
from Tkinter import *
def callback():
global root
root.withdraw()
win2 = Tk()
root = Tk()
Label(root,text='this is a window').pack()
Button(root,text='withdraw',command=self.callback).pack()
mainloop()
As soon as I press the button, the window disappears much as I want it, and another window appears and everything works great. How do I get the first window back, in the same state as it was before?
Use the following commands when you want to show the window:
# root.update() # not required
root.deiconify()
If you want to know more about it, see here.

Categories