tkinterdnd2 drag and drop not firing <<drop>> event - python

I'm trying to figure out why a <<drop>> event isn't firing after dragging a file into a tkinterdnd2 window.
I am running the following script with a Python 3.10.2 interpreter:
import tkinterdnd2
import tkinter as tk
from tkinter import messagebox
window = tkinterdnd2.Tk()
def drop(event):
path = event.data
messagebox.showinfo("Success", f"Path: {event.data}")
window.drop_target_register(tkinterdnd2.DND_FILES)
window.dnd_bind("<<drop>>", drop)
window.mainloop()
The Problem
When I drag a file onto the screen, the cursor changes to a plus sign (+), but releasing the file does not fire the event.
What I've Tried
Reinstalling tkinterdnd2 with pip
[Right Click] and "Open With" the correct Python interpreter
Adding a button to make sure that an event fires when clicked (It does)
Changing the drop target to an entry box
Restarting computer
Question
What am I doing wrong, or what can I do to make it right?

Related

Create new Tkinter Message Boxes when a button in an already existing Message Box

So I am trying to create 2 new message boxes when the "Ok" button is pressed in an already existing message box.
This is my code
from Tkinter import *
import tkMessageBox
window = Tk()
window.wm_withdraw()
#centre screen message
window.geometry("1x1+"+str(window.winfo_screenwidth()/2)+"+"+str(window.winfo_screenheight()/2))
tkMessageBox.showinfo(title="Hydra", message="Cut one head off, two more will grow back.")
def onclick():
window.geometry("1x1+"+str(window.winfo_screenwidth()/2)+"+"+str(window.winfo_screenheight()/2))
tkMessageBox.showinfo(title="Hydra", message="Cut one head off, two more will grow back.")
window.geometry("1x1+"+str(window.winfo_screenwidth()/2)+"+"+str(window.winfo_screenheight()/2))
tkMessageBox.showinfo(title="Hydra", message="Cut one head off, two more will grow back.")
Am I doing something wrong?
I'm running Python 3.7.0 on Windows 10, so details of my answer might not be correct.
Messagebox does not accept a command parameter which means you can not use a messagebox to start anything. Also, messagebox is a modal dialog which means it will suspend the main program until it's done. This means only one message box will be displayed at any time.
I think the easiest way would be to construct a non-modal custom dialog for this problem.

Python 3.6 crashes when clicking exit in Tkinter window Mac OSX

When trying to close my tkinter window using the red 'x' button (top left) the window doesn't close and python crashes(colour wheel).
(note this is across all of my Tkinter apps, not just the one shown)
Here is an image of my code:
Here is the crashed white box, neither "quit" or red "x" works. It doesn't show up in the screenshot but the spinning colour wheel is visible whenever I hover over my window. I have to force quit due to "Python not responding".
Got me thinking when I run python in the shell there's an error message: TK Tcl. Maybe unstable.
I suggest to try master.destroy instead of frame.quit for the callback of the Quit button, and give us feedback whether it changes anything

having trouble clicking in program - pyautogui

I'm trying to click into a program window using pyautogui.
When clicking in the program window, on a button I wish to press, a loading icon appears next to the mouse cursor as if it is thinking, and it never actually clicks. It does, however, move to the cursor location that I provide.
Here's the code
import pyautogui
pyautogui.doubleClick(x=300, y=300)
I opened Excel to see if it will click into that, and it does, but only if I run the code as:
import pyautogui
pyautogui.doubleClick(x=300, y=300)
pyautogui.doubleClick(x=300, y=300)
What the heck do I do?
If the application in running as administrator and pyautogui script is not running as administrators, then control will not work. pyautogui script should also run as administrator to control it
Try giving the interval argument
pyautogui.doubleClick(x=300, y=300, interval=0.25)

Tk open window issue/bug

If I type the following in the interpreter it works like a charm:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
fh = open(askopenfilename(), 'r')
However, if I write/save/run a script with exactly the same commands, though it works (kind of like expected) the open window goes blank and remains on screen (after opening the selected file) and stays on top of everything.
As a result I need to click on the Python icon again in order for the window to close. At one point this stopped happening, but when I ran a script once without the Tk().withdraw() command the problem re-emerged.
I am running OSX Mavericks. If there is no way to fix the bug, is there any command in Python I can implement that closes this window?
See the accepted answer to this question When do I need to call mainloop in a Tkinter application?. You normally need to call Tk.mainloop() to start the event loop processing for Tk. But when you are running in the interactive interpreter, Python calls the Tk event processor for you, otherwise you would not be able to use Tkinter in the interactive interpreter as easily.

How to intercept WM_DELETE_WINDOW on OSX using Tkinter

I'm trying to keep a Toplevel window from being closed in OSX by intercepting window manager WM_DELETE_WINDOW event.
#!/usr/bin/env python
from Tkinter import *
def speak():
print "woof"
root = Tk()
root.title("root")
win = Toplevel()
win.title("win")
win.protocol('WM_DELETE_WINDOW', speak)
root.mainloop()
When I run this I get two pop up windows titled "root" and "win". If I click on the red "x" close button on "win" to close the window, prints "woof" and then closes. However, if I run this same code on windows "win" stays open and keeps printing "woof" every time I click the red "x" close button.
How can I keep the Toplevel window from closing on OSX when I click the red "x" close button?
See the reply to Python Issue 12584. It appears to be a bug in the very buggy Cocoa Tcl/Tk 8.5 that Apple shipped with OS X 10.6. Don't use it or the Apple-supplied Pythons in 10.6 if you are using Tkinter or anything that uses Tkinter, like IDLE. More info here.

Categories

Resources