I'm developing an interface with Tkinter that makes use of a file dialog with tkFileDialog.
I want to run a function immediately after the user has chosen a file from the dialog box.
With buttons, we have a command keyword from which we run a function (usually named def callback():). Is there a similar keyword for the file dialog or askopenfilename?
The askopenfilename function consists of the opening of a dialog, and returns immediately when the latter was closed (including when a file has been selected).
Put your callback right after this function to have it run right after the closing of the dialog.
For instance:
from tkinter.filedialog import askopenfile
fileDescriptor = askopenfilename()
print(fileDescriptor)
will open a file selection dialog, and as soon as the user has selected a file, the corresponding object that was created will be printed out.
Related
I am trying to make a Tkinter script to select files through Windows File Explorer. I don't need the Tkinter window to show, just the File Explorer interface.
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
#window.geometry("1x1")
window.withdraw()
def open_files():
files = filedialog.askopenfiles(mode='r')
global filenames
filenames = [os.path.abspath(file.name) for file in files]
window.destroy() # analysis:ignore #says "window" is undefined becasue of "del window" below
window.after(0, open_files)
window.mainloop()
del window
The first time I run this in Spyder, if window.withdraw() is not commented out, the console just shows runfile(*my_file_name*) and the code does... something... in the background, but nothing seems to actually happen. Nothing changes on-screen, but I cannot type in the console so I know the code is running.
If I open a new console tab and run the code with window.withdraw() commented out, everything works, and the Tkinter GUI is visible. If I then run this code again in the same tab, with window.withdraw() not commented out, then the code works as intended, with only the File Explorer window opening up, and the Tkinter GUI staying hidden. This is the case even if I click the "Remove all variables" button in Spyder, so as far as I understand the code is not saving any variables that allow it to run properly after the first time.
My question is, why does this code work the 2nd, 3rd, 4th, etc. time I run it, but not the first time?
I kept playing around, and changed -alpha to alpha and got this error:
TclError: wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"
So I ended up changing window.attributes('-alpha',0) to window.attributes('-topmost',True, '-alpha',0), and this works! It brings up File Explorer on the first run without showing the Tkinter window. Thank you #Thingamabobs for your help.
My final code is:
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
window.attributes('-topmost',True, '-alpha',0)
filenames = [os.path.abspath(file.name) for file in filedialog.askopenfiles(mode='r')]
window.destroy()
del window
So I made the Main Menu in one Tkinter File and I made an app in another file. In the main menu, I have a button, when I click that button it should open the app but NOT SHOW THE MAIN MENU.
File 1
from tkinter import *
root = Tk()
root.geometry("600x600")
btn = Button(root, command=#add one later)
And in file 2 I have my app and when I click on the button it should open the app and hide the main menu. Can I do this through frames?
NOTE: THE APP IS ANOTHER FILE
As far as I have understood your question, there could be 2 things that you might be asking, 1st if you want to launch a new window and destroy this, you can call root.destroy() followed by calling the other file by using import file_name that has another Tk(). 2nd if you want to pack/update a Frame then make sure that the other file is within a definition which is not called there, also in the second file use the same terminology as you have used in the first file or you can pass these parameters through the function call. Now you can do
menu.destroy()
import file_name
file_name.function_name(arguments)
I am new in tkinter please help me out .
I have implemented a module(PDF2Text.Py) that its class has a function (convert_pdf_to_txt(path)) that takes a path of a pdf file and converts the pdf file into text.
I also implemented another module(TopicModeling.py) that its class has a function (creat_LDA_model(text)) that takes a text and do topic modeling on the text.
Now, I want the tkinter GUI that is, upon clicking the "Browse" button it browses the path with filedialog.askopenfilename and its command function send the given path to convert_pdf_to_txt(path) function of PDF2Text.Py.
Then by clicking the "Model" button, its command function returned text should be sent to creat_LDA_model(text) function in TopicModeling.py and show the result in an Entry widget or any other widget types .
I would like to know the structure of the GUI module;
how to call or get and set the parameters to other modules/functions from the GUI module in command functions of the buttons.
thanks
Seems like you have multiple questions here. I believe one of them is how to start askopenfilename in a particular folder. Once the file name is returned, i can be passed to another function.
fname = askopenfilename(initialdir = r'c:\somepath', filetypes=(("Statement files", "*.pdf"),))
To call other functions you have written, import the module, let's cal it ReadPdf.py, use something like this.
import ReadPdf
PdfOut = ReadPdf.ReadPDFData(fname)
CCStmtTxns = ReadPdf.ReadPDFData(CreditCardPDFDataPathandFile)
I am trying to write a simple text editor using TkInter. I want it to have a menu bar like other text editors, where you can save, open another file, etc.
However, whenever I try to add a menu bar to my class, the program simply starts, hangs for about half a second, then exits. I have no idea why this is happening, or how to debug it. Here is my code.
#!/usr/bin/env python3
import functools
from tkinter import *
class mainWindow(Tk):
def initiate(self):
menuBarFrame = Frame(self).pack(side=TOP)
menubar = Menu(menuBarFrame)
menubar.add_command(label='Exit', command=quit())
root.config(menu=menubar)
mainloop()
win = mainWindow().initiate()
I tried adding .pack() to the line
menubar = Menu(menuBarFrame)
but it gives me the following traceback:
File "XML.py", line 14, in <module>
win = mainWindow().initiate()
File "XML.py", line 9, in initiate
menubar = Menu(menuBarFrame).pack()
File "/usr/lib/python3.4/tkinter/__init__.py", line 1977, in pack_configure
+ self._options(cnf, kw))
_tkinter.TclError: can't pack ".140664986043280": it's a top-level window
When I remove the code for the menubar, and just replace it with a simple button, the application works and starts fine. What could be causing the problem?
The menu needs to be a child of the root window, rather than a child of a frame. You don't need MenuBarFrame at all.
Also, take a look at this line:
menubar.add_command(label='Exit', command=quit())
You are instructing Tkinter to immediately call the quit() function, and assign the result to the command attribute of the menu command. I'm guessing that quit() actually quits rather than returning a reference to some other function. You need to change it to this:
menubar.add_command(label='Exit', command=quit)
Of course, the other glaring problem is that you don't actually define root anywhere.
You definitely don't want to call pack() on the instance of Menu. The correct way to attach the menu to the window is with root.config(menu=menubar), like you're already doing.
To allow my users to choose a file I'm using tkinter's askopenfilename. The function works fine, but after opening a file or pressing cancel the file open window goes empty and just stays open. The rest of the program moves forward correctly but even once the program ends the window remains open. And I'm not getting any errors. Is there anyone else who has experienced this behavior and has a solution? Alternatively, do you know any other easy methods of implementing a GUI file open prompt?
here's a pertinent example:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)