I building a messenger with possibility to send files. Iv'e added this line to prevent the tk window to be shown:
Tk().withdraw()
and I'm opening files with this command
tkFileDialog.askopenfilename()
through IDLE python editor it all seems to be working, but when I open python script through cmd and try to send a file , the gui do not show up at all, and the dialog to choose file doesn't open.
when I remove the command Tk().withdraw() , it seems to be working again , but this stupid blank Tk window is still shown up.
How do I prevent the blank Tk from being shown up when I running the script from cmd without causing the choose file dialog to disappear?
this is the code (I'm using python 2.7 , and this is not the whole code, I pasted just the necessary parts):
from Tkinter import Tk
import tkFileDialog
messege = raw_input()
if messege == "SEND":
print "Starting to send file..."
isSendingFile = True
resDir = tkFileDialog.askopenfilename()
filetype = resDir[(resDir.rfind('.')):]
filename = resDir[(resDir.rfind('/') + 1):-(len(filetype))]
s.send("SEND|name:" + filename + "|type:" + filetype)
fileToSend = open(resDir, "rb")
messege = fileToSend.read()
s.send(messege)
fileToSend.close()
isSendingFile = False
You could try to keep a reference to the main window:
import os
try: # Python 2
from Tkinter import Tk
from tkSimpleDialog import askstring
from tkFileDialog import askopenfilename
except ImportError: # Python 3
from tkinter import Tk
from tkinter.simpledialog import askstring
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw() # hide main window
message = askstring("Message", "What is the message?")
if message == "SEND":
resdir = askopenfilename()
root.destroy() # done with gui
path, filetype = os.path.splitext(resdir)
filename = os.path.basename(path)
Related
When I run my code, I can get any file filepath for further use and put it in the Entry box. When I try to get a filepath of a shortcut, another window opens up, saying "catastrophic error". The console remains quiet all the time. Here's my code:
from tkinter import *
from PIL import *
import os
from tkinter import filedialog
root = Tk()
def create_game():
# Defining the function for choosing the filepath
def add_data():
root.filename = filedialog.askopenfilename(initialdir="",title="Select A File",filetypes=((".EXE files", "*.exe"),("all", "*.*")))
enter_filepath.insert(0,str(root.filename))
enter_filepath = Entry(root,width=30)
enter_filepath.pack()
btn_filepath = Button(root,text="Choose file",command=add_data)
btn_filepath.pack()
create_game()
root.mainloop()
This is what I'm getting:
import tkinter as tk
from tkinter import filedialog
root=tk.Tk()
root.withdraw()
path = filedialog.askdirectory()
test_str = input('Input for test: ')
Environment is Windows - cmd.
After filedialog, cmd doesn't get focus back to appopriate place - input() - after Input for test:.
Is this problem of Windows cmd?
Or just am I doing wrong?
I am using tkinter asopenfilename to trigger a file chooser so as to read files from my local directory. My problem is that after the file is chosen, the window freezes and python is 'not responding'.
I've tried the answer from this post: Tkinter askopenfilename() won't close no luck.
Below is my code:
from tkinter import Tk
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw()
root.update()
filename = askopenfilename()
print(filename)
Is there anything that I am missing? Let me know if you need more clarity. Thanks!
I tried all above solutions but didn't seem to solve the same issue for me. The dialog box was opening but somewhere in the background.
Found this code elsewhere and it works like a charm for me. On windows 10 too, python 3.x, and using Jupyter Notebook.
Posting it here in case it could help others.
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
root.call('wm', 'attributes', '.', '-topmost', True)
file_path = filedialog.askopenfilename(
%gui tk
print(file_path)
I tried using root.update() after filename = askopenfilename() in my MacOS.
Following worked for me-
from tkinter import Tk
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw()
filename = askopenfilename()
root.update()
print(filename)
I had the same behavior on MacOS and adding the iPython magic %gui tk appears to be solving the issue:
from tkinter import Tk
from tkinter.filedialog import askopenfilename
%gui tk
root = Tk()
root.withdraw()
filename = askopenfilename(multiple=True)
print(filename)
From the docs:
%gui tk # enable Tk event loop integration
askopenfilename doesn't work in windows
from tkinter import *
#from tkFileDialog import askopenfilename
import tkinter.filedialog
def callback():
name= tkinter.filedialog.askopenfilenames()
print (name)
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
I am using python 3.6 and have the following issue.
These lines of codes apparently open two windows, but I only expect one to be opened.
from tkinter.filedialog import asksaveasfilename
file_name = asksaveasfilename()
root.withdraw()
IIRC, you're supposed to call withdraw before you call asksaveasfilename. Same as in Choosing a file in Python with simple Dialog.
import tkinter
from tkinter.filedialog import asksaveasfilename
tkinter.Tk().withdraw()
file_name = asksaveasfilename()
when you do filename = asksaveasfilename() you call the function
and that makes tkinter open the second window put that in a function and on
a button click the second window will open
from tkinter.filedialog import asksaveasfilename
import tkinter
root = tkinter.Tk()
root.withdraw()
asksaveasfilename()
I'm trying to open a simple file dialog to ask the user to select a file. The code I have is:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)
sys.exit(0)
The program retrieves the file name successfully, however the window remains open and does not close. The only way I can exit it is through Force Quit. I am using Mac OS X 10.11 and Python 2.7.11.
Thank you
There seems to be some issues based on your development environment. See reference [2]. This worked for me:
from Tkinter import Tk
from tkFileDialog import askopenfilename
root = Tk() #To initialize Tkinter, we have to first create a Tk root widget
filename = askopenfilename() # store filename
# root.mainloop() may be necessary for your development environment (see [2])
root.destroy() #destroy the event loop - only required in some python environments (see [2])
print(filename)
[1] http://effbot.org/tkinterbook/tkinter-hello-tkinter.htm
[2] http://effbot.org/tkinterbook/tkinter-hello-again.htm