I want to make a Tkinter interface for a program I just wrote. The interface needs to have a widget where the user can insert an image for the program to process. I couldn't find such a widget online. How would I make such an interface?
Below is an example of tkinter entry widget that takes in images right away:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
import tkinter.filedialog as tkfd
except ImportError:
import Tkinter as tk
import tkFileDialog as tkfd
if __name__ == '__main__':
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
path = tkfd.askopenfilename(initialdir = "/", title = "Select file",
filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
entry.insert('0', path)
root.mainloop()
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:
I'm trying to add the ScrolledText widget to a Tkinter window. The program reads it perfectly as in it accepts the INSERT method for it with no errors but it's not showing up. The problem came up when I added Notebook Tabs. I've attached the code snippet. I used the place() method because I need the rest of my buttons and labels arranged in a specific pattern.
import tkinter
from tkinter import *
from tkinter import scrolledtext
from tkinter import messagebox
from tkinter import ttk
import os
import datetime
# Variables
window = Tk()
window.title("Vesnica Pomenire")
window.geometry('1500x1000')
var = IntVar()
var.set(1)
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.place(x=50, y=50)
You're missing the mainloop()
import tkinter
from tkinter import *
from tkinter import scrolledtext
from tkinter import messagebox
from tkinter import ttk
import os
import datetime
# Variables
window = Tk()
window.title("Vesnica Pomenire")
window.geometry('1500x1000')
var = IntVar()
var.set(1)
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.place(x=50, y=50)
window.mainloop() #You are missing this
You can read more about mainloop() here
You really missed mainloop command
window.mainloop()
add this at the bottom of your code and it will do the thing
trying to make a GUI with an 'open file' button. When I run the code shown below, the open file dialog opens straight away, and not when I press the button. Why? Is there a simple way to fix this that doesn't involve using classes? (I don't currently know anything about classes and am working on a time-pressured project)
from tkinter import *
interface = Tk()
def openfile():
return filedialog.askopenfilename()
button = ttk.Button(interface, text = "Open", command = openfile())
button.grid(column = 1, row = 1)
interface.mainloop()
The code is passing the return value of the openfile function call, not the function itself. Pass the function itself by removing trailing () which cause a call.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
interface = Tk()
def openfile():
return filedialog.askopenfilename()
button = ttk.Button(interface, text="Open", command=openfile) # <------
button.grid(column=1, row=1)
interface.mainloop()
I use this to get yes/no from user but it opens an empty window:
from Tkinter import *
from tkMessageBox import *
if askyesno('Verify', 'Really quit?'):
print "ok"
And this empty window doesnt go away. How can I prevent this?
This won't work:
Tk().withdraw()
showinfo('OK', 'Select month')
print "line 677"
root = Tk()
root.title("Report month")
months = ["Jan","Feb","Mar"]
sel_list = []
print "line 682"
def get_sel():
sel_list.append(Lb1.curselection())
root.destroy()
def cancel():
root.destroy()
B = Button(root, text ="OK", command = get_sel)
C = Button(root, text ="Cancel", command = cancel)
Lb1 = Listbox(root, selectmode=SINGLE)
for i,j in enumerate(months):
Lb1.insert(i,j)
Lb1.pack()
B.pack()
C.pack()
print "line 702"
root.mainloop()
for i in sel_list[0]:
print months[int(i)]
return months[int(sel_list[0][0])]
Tkinter requires that a root window exist before you can create any other widgets, windows or dialogs. If you try to create a dialog before creating a root window, tkinter will automatically create the root window for you.
The solution is to explicitly create a root window, then withdraw it if you don't want it to be visible.
You should always create exactly one instance of Tk, and your program should be designed to exit when that window is destroyed.
Create root window explicitly, then withdraw.
from Tkinter import *
from tkMessageBox import *
Tk().withdraw()
askyesno('Verify', 'Really quit?')
Not beautiful solution, but it works.
UPDATE
Do not create the second Tk window.
from Tkinter import *
from tkMessageBox import *
root = Tk()
root.withdraw()
showinfo('OK', 'Please choose')
root.deiconify()
# Do not create another Tk window. reuse root.
root.title("Report month")
...
in python's default editor, IDLE, it is possible to have multiple 'Open' dialogs opened at the same time.
I'm looking at their source, but I can't find where I can have this behavior replicated. from their IOBinding.py's :
from tkinter import filedialog as TkFileDialog
...
class IOBinding:
...
def askopenfile(self):
dir, base = self.defaultfilename("open")
if not self.opendialog:
self.opendialog = tkFileDialog.Open(master=self.text,
filetypes=self.filetypes)
filename = self.opendialog.show(initialdir=dir, initialfile=base)
return filename
so they do use tkinter's built-in filedialog module, but I can't find way to have some 'modeless' dialogs. I can have dialogs opened by two codes, which are basically same:
from tkinter import filedialog as tkFileDialog
file_name = tkFileDialog.Open( ... ).show()
file_name = tkFileDialog.askopenfilename()
but they blocks whole application - users cannot switch windows or issue new command until they close the dialog. also, I can't call these dialog functions from different thread - that will kill whole my Tk app. What should I do?
filedialog have parent option. You can change it to hidden window to prevent blocking the root window:
from tkinter import filedialog as tkFileDialog
from tkinter import *
def ask_open():
p = hidden if attach_to_hidden.get() else root
tkFileDialog.Open(parent=p).show()
root = Tk()
hidden = Toplevel()
hidden.withdraw()
attach_to_hidden = IntVar()
Checkbutton(root, text='Attach to hidden window', variable=attach_to_hidden).pack()
Button(root, text='Open', command=ask_open).pack()
root.mainloop()