Assumption: I'm using Python 3.6 and I'm working on Windows 10
Is possible to create a GUI with tkinter in which dragging a file in the window it returns the path of the file?
If with tkinter it's not possible, is there another solution that can solve the problem without installing additional libraries?
You need to install tkinterdnd2
pip install tkinterdnd2
code:
from tkinter import TOP, Entry, Label, StringVar
from tkinterdnd2 import *
def get_path(event):
pathLabel.configure(text = event.data)
root = TkinterDnD.Tk()
root.geometry("350x100")
root.title("Get file path")
nameVar = StringVar()
entryWidget = Entry(root)
entryWidget.pack(side=TOP, padx=5, pady=5)
pathLabel = Label(root, text="Drag and drop file in the entry box")
pathLabel.pack(side=TOP)
entryWidget.drop_target_register(DND_ALL)
entryWidget.dnd_bind("<<Drop>>", get_path)
root.mainloop()
Related
I am eriting code in pycharm with tkinter but the window is not opening. May someone assist?
`
import tkinter
window = tkinter.Tk()
button = tkinter.Button(window, text="Do not press this button! >:-(", width=40)
button.pack(padx=10, pady=10)
`
i tried checking my script for bugs but nothing
This has nothing to do with Pycharm, but with tkinter library and how to use it.
You are missing 2 important stuff:
Button is in ttk.py file inside tkinter library: from tkinter import ttk
Execute the whole script with mainloop
Try this:
import tkinter
from tkinter import ttk # Import ttk file from tkinter library
window = tkinter.Tk()
window.title("Coolest title ever written")
button = ttk.Button(window, text="Do not press this button! >:-(", width=40) # Use Button from the import ttk file
button.pack(padx=10, pady=10)
window.mainloop() # Execute the whole script
I have tried to open this program with double click using python application but it doesn't work.
I test it and I think the problem is the use of ImageTk.
from tkinter import *
from PIL import ImageTk,Image
root = Tk()
root.title('Images')
root.iconbitmap('G_image.ico')
my_img = ImageTk.PhotoImage(Image.open('2021_1.png'))
my_label = Label(image=my_img)
my_label.pack()
button_quit = Button(root,text='Exit Program',command=root.destroy)
button_quit.pack()
root.mainloop()
Do you have any errors?
But I guess it's because you have to "pack" your label in the root window:
my_label = Label(root, image=img)
my_label.pack()
and to double click on the python file, try this:
right click on the file->open with->choose default program->more options->select python.exe file and click on.
Hi I am a novice programmer.I am trying to use tkinter in pycharm....
When i try to use root or any other function it doesn't show any suggestion and gives not defined error.The program works fine in idle. But cann't seem to get it work with pycharm.Please help.I have already installed tkinter package and its also enabled in project interpreter settings....
here's what I am trying to do...
from tkinter import *
root = Tk()
topframe = Frame(root)
topframe.pack()
bottomframe = Frame(root)
bottomframe.pack(side = BOTTOM)
button1 = Button(topframe, text='Button 1', fg='red')
button2 = Button(topframe, text='Button 2', fg='blue')
button3 = Button(bottomframe, text='Button 3', fg='green')
button1.pack(side = LEFT)
button2.pack(side = LEFT)
button3.pack()
root.mainloop()
Just try this
from tkinter import Tk
root= Tk()
I also faced this problem before. This worked for me. I don't know the reason.
Note Tkinter has been renamed to tkinter in Python 3
tkinter — Python interface to Tcl/Tk.(Tk itself is not part of Python; it is maintained at ActiveState)
source : official Doc
Also there are certain classes that don't get imported when you try to import with *. When working with tkinter.
turn Around:
Solution 1.
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
root = tk.Tk()
Solution 2:check correct version is installed in pycharm.
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
root.mainloop()
Solution 3: Absolute Import
from tkinter import Tk
root= Tk()
w = Label(root, text="Hello, world!")
I had the same problem. I found that when I typed from tkinter there were 2 different options; you need to choose the option with a file icon next to it
I'm trying to make a simple outline for a gui, and I'm getting the warning
"variable" May be undefined or defined from star imports: tkinter for all of my variables.
Here is my code:
from tkinter import *
class myApp :
def __init__(self, gui,) :
self.root = gui
self.bframe = Frame(self.root) # Create a container Frame at bottom
self.bframe.pack(side=BOTTOM)
self.xlabel = Label(self.root, text="Item ID") # Create the Label
self.xlabel.pack(side=LEFT)
self.xentry = Entry(self.root, bd=5) # Create the Entry box
self.xentry.pack(side=LEFT)
self.xentry.bind('<Return>', self.showStockItem)
self.xentry.focus_set() # Set focus in the Entry box
self.xopen = Button(self.root, text="Show", command=self.showStockItem) # Create the open Button
self.xopen.pack(side=LEFT)
self.xquit = Button(self.bframe, text="Quit", command=self.quitit) # Create the quit Button
self.xquit.pack(side=BOTTOM)
return
gui = Tk()
gui.title("Travel")
app = myApp(gui)
gui.mainloop()
from tkinter import *
In this line, you import everything from tkinter. This is not recommended, so linter will warn you. But if you really want to do this, it's OK, just ignore it.
To be better, you should explicitly import what you need. For example:
from tkinter import Tk, Label, Frame, Entry, Button
Consider using:
import tkinter as tk
and then, prefix all your calls like:
root = tk.Tk()
or,
variableName.pack(side = tk.LEFT)
and so on...
from tkinter import *
photo = PhotoImage(file="C:\Temp\test\computer.gif")
lbl = Label(root, image=photo, height="10", width="20").pack
I have absolutely no idea why this won't work it comes up with: _tkinter.TclError: couldn't recognize data in image file "C:\Temp\test\computer.gif.
Windows filenames always have to be entered as raw strings (in all of python, not just with tkinter). Also, you'll have to make a root window first. Try this:
from tkinter import *
root = Tk()
photo = PhotoImage(file=r"C:\Temp\test\computer.gif")
Label(root, image=photo, height="10", width="20").pack()
root.mainloop()