Choosing file by clicking on a button [Python 2.7.6] - python

I'm trying to create a simple button that allows me to choose files such as text doc/pictures(jpg/png). I tried searching for answers here but didn't had any luck. I'm using Tkinter for my GUI interface.
This are my codes so far.
from Tkinter import *
root = Tk()
root.title("Hashing Tool")
root.geometry("600x300")
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
button = Button(frame, text="Choose File", fg="black")
button.pack( side = BOTTOM)
from tkFileDialog import askopenfilename
filename = askopenfilename()
print(filename)
root.mainloop()

Currently, you ask for a file as soon as the program starts. You have to put that part of the code into a callback function and pass that to the button's command parameter.
def getfile():
filename = askopenfilename()
print(filename)
button = Button(frame, text="Choose File", fg="black", command=getfile)

Related

How can I assign the text imputed into an entry box (tkinter) to variable in python script and run the script by pressing a button?

I have a python script that takes the file path and runs the following script:
file = 'C:/Users/crist/Downloads/Fraction_Event_Report_Wednesday_June_16_2021_20_27_38.pdf'
lines = []
with pdfplumber.open(file) as pdf:
pages = pdf.pages
for page in pdf.pages:
text = page.extract_text()
print(text)
I have created an entry box with tkinter:
import tkinter as tk
master = tk.Tk()
tk.Label(master,
text="File_path").grid(row=0)
e = tk.Entry(master)
e.grid(row=0, column=1)
tk.Button(master,
text='Run Script',
command=master.quit).grid(row=3,
column=0,
sticky=tk.W,
pady=4)
tk.mainloop()
I would like to assign the File_path imputed in the entry box by the user to the "file" in the script and run the script when pressing the "Run Script" button. How can I do that?
It's better to spawn a file dialog instead of using a tkinter.Entry:
# GUI.py
from tkinter.filedialog import askopenfilename
import tkinter as tk
# Create the window and hide it
root = tk.Tk()
root.withdraw()
# Now you are free to popup any dialog that you need
filetypes = (("PDF file", "*.pdf"), ("All files", "*.*"))
filepath = askopenfilename(filetypes=filetypes)
# Now use the filepath
lines = []
with pdfplumber.open(filepath) as pdf:
...
# Destroy the window
root.destroy()

tkinter - open file with button and save filename to variable

I am trying to solve a problem that seems simple, but I cannot figure out how.
I want to create a simple program checking if a certain symbol exists in a text file:
The program starts;
The user clicks a button (inside the window, not in the menu);
A dialog box appears;
The user chooses a text file;
A message box displays the result;
The program closes.
Pretty straightforward, but I cannot find how to save the filename into a variable and then use it for the process. I read so many tutorials and I could not find a solution. Here is the code:
from tkinter import *
from tkinter import filedialog
def clicked():
global filename
filename = filedialog.askopenfile(filetypes=(("Word files","*.docx"),))
window = Tk()
window.geometry()
window.title("My App")
open_file_label = Label(window, text="Open your docx file here:", font=("Arial", 10), padx=5, pady=5)
open_file_label.grid(column=0, row=0)
open_file_button = Button(window, text="Click me", command=clicked, padx=5, pady=5)
open_file_button.grid(column=1, row=0)
window.mainloop()
filename is already a variable that contains the chosen file's contents. Just print(filename) and you'll get your data printed in console.

python tkinter clicking on button to open new window

I've made 3 buttons on my window. I choosed that the main window should have a specific background image and a full screen.
Now there is a problem. I would like to move to a new window (page) (with an other background and other things) by clicking on button 3.
Things i tryd:
from Main.Info.travelhistry import *
I've added this to the main window to open a new python file with the code of the second screen that has to open when clicking on button 3. But I found out that if I do this both windows will open when running main window.
I added root1 = Tk() at the beginning, root1.mainloop() at the end and between them the code for the other window. But this won't work also, its opening 2 windows like above.
Those were all my attempts and i cant figure out a better way. I can but the background would stay the same. But I have to change the background for the new window to a background image i made...
Any idea what im doing wrong?
from tkinter import *
from tkinter.messagebox import showinfo
from Main.Info.travelhistry import *
def clicked1():
bericht = 'Deze functie is uitgeschakeld.'
showinfo(title='popup', message=bericht)
root = Tk()
a = root.wm_attributes('-fullscreen', 1)
#Hoofdmenu achtergrond
C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file = "test1.png")
background_label = Label(root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
# Geen OV-chipkaart button
b=Button(master=root, command=clicked1)
photo=PhotoImage(file="button1.png")
b.config(image=photo,width="136",height="53", background='black')
b.place(x=310, y=340)
#Buitenland button
b2=Button(master=root, command=clicked1)
photo1=PhotoImage(file="button2.png")
b2.config(image=photo1,width="136",height="53", background='black')
b2.place(x=490, y=340)
#Reis informatie
b3=Button(master=root)
photo2=PhotoImage(file="button3.png")
b3.config(image=photo2,width="136",height="53", background='black')
b3.place(x=680, y=340)
root.mainloop()
root2.mainloop()
You shouldn't call more than one Tk() window.
Instead, tkinter has another widget called Toplevel which can be used to generate a new window.
See below for an example:
from tkinter import *
root = Tk()
def command():
Toplevel(root)
button = Button(root, text="New Window", command=command)
button.pack()
root.mainloop()
This one opens new window that you can edit.
from tkinter import *
Window = Tk()
def Open():
New_Window = Tk()
#You can edit here.
New_Window.mainloop()
Btn1 = Button(text="Open", command=Open)
Bt1n.pack()
Window.mainloop()

No input possible after tk

If have this piece of code:
import Tkinter as tk
import tkFileDialog
menu = tk.Tk()
res = tkFileDialog.askopenfilename() # un-/comment this line
label = tk.Label(None, text="abc")
label.grid(row=0, column=0, sticky=tk.W)
entry = tk.Entry(None)
entry.grid(row=0, column=1, sticky=tk.EW)
res = menu.mainloop()
Note: the askopenfilename is just a dummy input. So Just close it to get to the (now blocked) main window of TK.
When I comment the askopenfilename everything works fine. But with the it, I can not enter data in the entry.
This only happens with Windoze environments. The askopenfilename seems to steal the focus for the main TK window. After clicking a totally different window and back again in the TK window, input is possible.
I've seen reports of this before, I think it's a known bug on windows. You need to let mainloop start before you open a dialog.
If you want the dialog to appear when the app first starts up you can use after or after_idle to have it run after mainloop starts up.
For example:
menu = tk.Tk()
...
def on_startup():
res = tkFileDialog.askopenfilename()
menu.after_idle(on_startup)
menu.mainloop()
If you don't want any other GUI code to execute until after the dialog, move all your code except for the creation of the root window and call to mainloop into on_startup or some other function.
For example:
def main(filename):
label = tk.Label(None, text="abc")
label.grid(row=0, column=0, sticky=tk.W)
entry = tk.Entry(None)
entry.grid(row=0, column=1, sticky=tk.EW)
def on_startup():
res = tkFileDialog.askopenfilename()
main(filename)
root = Tk()
root.after_idle(on_startup)
askopenfilenamehas it's own event loop. The programm stops, until you selected a filename, and continues afterwards.

Custom Python GUI Button + Link

I am learning the basics of python GUI and I came across button making. How would I link a file that I have done in photoshop to be the button? And when you click on it, it takes you to say, google?
This is what I have:
from Tkinter import *
root = Tk()
root.title("Simple GUI")
root.geometry("800x600")
app = Frame(root)
app.grid()
button1 = Button(app, text "Sample Button!")
button1.grid()
root.mainloop()
You can use a bitmap image. Here's a link for a tutorial.
Use something like the following:
from Tkinter import *
import ImageTk, webbrowser
def launchGoogle():
webbrowser.open('http://www.google.com')
root = Tk()
root.title("Simple GUI")
root.geometry("800x600")
app = Frame(root)
app.grid()
icon = ImageTk.PhotoImage(file="logo11w.png")
button1 = Button(app, text="Sample Button!", image=icon, command=launchGoogle)
button1.grid()
root.mainloop()

Categories