This question already has answers here:
No input possible after tk
(2 answers)
Closed 4 years ago.
I'm using the following code to open a file, read its lines into a list and filter them using a substring from an Entry:
def get_entries(self):
"""
Open a file and load entries into a list.
"""
try:
# self.file_name = "p1.py"
self.file_name = askopenfilename(title="Open file")
self.file_handle = open(self.file_name, "r")
except IOError:
messagebox.showinfo("Info", "No file has been openned.")
self.destroy()
else:
self.entry_list = self.file_handle.readlines()
self.update_list()
def update_list(self, *args):
"""
Update the list after each editing of the search filter
"""
search_term = self.search_var.get()
self.lbox.delete(*self.lbox.get_children())
for index, item in enumerate(self.entry_list):
if search_term.lower() in item.lower():
self.lbox.insert('', END, values=(index, item))
Why does it work fine using self.file_name = "p1.py" but using askopenfilename() disables editing the Entry?
Minimizing and restoring the window with the Entry fixes the problem.
I'm using PyCharm on Windows 10
Here is the rest of the code for reference:
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from tkinter.filedialog import askopenfilename
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.file_name = ""
self.file_handle = ""
self.entry_list = None
self.search_label = Label(self, text="Filter: ")
self.search_var = StringVar()
self.search_var.trace("w", self.update_list)
self.search_entry = Entry(self, textvariable=self.search_var)
self.lbox = ttk.Treeview(self, columns=('indices', 'entries'), displaycolumns='entries', show='headings')
self.lbox.heading('entries', text="Entries", anchor="w")
self.confirm = Button(self, text="Confirm", width=10, command=self.confirm_action)
self.cancel = Button(self, text="Cancel", width=10, command=quit)
self.search_label.grid(row=0, column=0, sticky=E, padx=12, pady=5)
self.search_entry.grid(row=0, column=1, sticky=W, columnspan=4, pady=5)
self.lbox.grid(row=1, column=0, columnspan=3, sticky=(N, W, S, E), padx=12, pady=5)
self.cancel.grid(row=2, column=0, pady=5)
self.confirm.grid(row=2, column=1, sticky=W, padx=12, pady=5)
self.grid_columnconfigure(0, weight=1, uniform="u")
self.grid_columnconfigure(1, weight=1, uniform="u")
self.grid_columnconfigure(2, weight=4, uniform="u")
self.get_entries()
def get_entries(self): ...
def update_list(self, *args): ...
def confirm_action(self): ...
root = Tk()
root.title('Filter Listbox Test')
app = Application(master=root)
app.mainloop()
This seems to be a problem due to calling the askfilename before the root window is drawn. As a workaround you can add self.update() before you call askopenfilename.
class Application(Frame):
def __init__(self, master=None):
# ... stuff ...
self.update()
self.get_entries()
I'll file a bug report about this right now.
Related
I'm quite new to Python and have a problem which I can't solve.
I want to write a Class which displays a configuration file (dict) in a tkinter frame. It should choose a tk.widged by type of value and for bool values create checkboxes, and for int and strings it should create entries.
After all it should give back a dict with the same keys, but the values changed to tk.*Var()
However, with the checkboxes there is no problem.
But the int and strings from the entries will not be written through the save_config() function.
It seems the tk.IntVar() and the tk.StringVar() don't update when typing something in the entries.
Hope my question is clear.
Can anybody please help?
code updated like asked by Bryan Oakley:
It also contains the changes suggested by Martin Finke which brings the solution for me!!
#!/usr/bin/env python3.9
import tkinter as tk
from tkinter import ttk
class MainFrame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self)
self.test_dict = {"name": "Ares"}
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=0)
self.save_config_button = ttk.Button(
self, text="Print Configuration", command=self.save_config)
self.save_config_button.grid(row=0, column=0, padx=5, pady=5)
self.configuration_frame = ConfigurationFrame
self.config_frame = self.configuration_frame(
self, "Configurations", self.test_dict)
self.config_frame.grid(row=1, column=0, padx=5, pady=5)
self.configuration = self.config_frame.get_configuration()
def save_config(self):
conf = {}
for key, value in self.configuration.items():
conf[key] = value.get()
print(conf)
class ConfigurationFrame(tk.LabelFrame):
def __init__(self, parent, widget_name, config, * args, **kwargs):
tk.LabelFrame.__init__(
self, parent, * args, **kwargs)
self.config(bd=2, text=widget_name)
self.grid(sticky=tk.NSEW)
self.configuration = {}
self.rowconfigure(0, weight=0)
self.columnconfigure(0, weight=1)
count = 0
for key, value in config.items():
if type(value) == str:
name = key
entry_text = tk.StringVar(self)
entry_text.set(value)
frame_label = tk.Frame(
self, height=1)
frame_label.grid(
row=count, column=0, padx=10, pady=2, sticky=tk.W)
self.entry_str = ttk.Entry(
frame_label, textvariable=entry_text, text=name, width=10)
self.entry_str.grid(row=0, column=0, padx=5,
pady=2, sticky=tk.W)
self.entry_str.insert(tk.END, str(value))
self.name_label = tk.Label(
frame_label, text=name)
self.name_label.grid(row=0, column=1, padx=5,
pady=2, sticky=tk.W)
self.configuration[name] = self.entry_str
count += 1
def get_configuration(self):
return self.configuration
def main():
MainFrame().mainloop()
if __name__ == "__main__":
main()
Thanks to enyone!
I suggest a simple change in the __ init __ function of the ConfigurationFrame class:
line self.configuration[name] = entry_int_txt replace self.configuration[name] = self.entry_int
line self.configuration[name] = entry_text replace self.configuration[name] = self.entry_str
So before you question the code, im just trying to learn tkinter a bit more so i though this be best way haha.
I am currently stuck on the web input, everytime i input a website it comes as "https://%21browser.%21text!" and i dont know why. Random web works but it doesnt work as inputting it.
import webbrowser as wb
from tkinter import *
from tkinter import ttk
import tkinter as tk
import ctypes, random, time
class Browser(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.start()
def start(self):
global webinput
self.title = Label(self, text="Web Shortcut", fg="purple", font="Kokila 20 bold")
self.title.pack(fill=BOTH)
self.webinput = Text(self, height=1, width=57)
self.webinput.pack(side=TOP, fill=BOTH, pady=30, padx=30)
self.openweb = Button(self, height=2, width=20, text="Open Web", bg="gray", fg="lightgreen", command=self.web)
self.openweb.pack(side=LEFT, padx=10)
self.random = Button(self, height=2, width=20, text="Random Web", bg="gray", fg="lightgreen", command=self.randomweb)
self.random.pack(side=LEFT, padx=5)
self.exit = Button(self, height=2, width=20, text="Exit", bg="gray", fg="lightgreen", command=exit)
self.exit.pack(side=LEFT, padx=5)
def web(self):
try:
self.sites = "https://{}".format(self.webinput)
if self.sites:
wb.open(self.sites)
except Exception as e:
ctypes.windll.user32.MessageBoxW(0, str(e), "CRASHED", 0)
def randomweb(self):
self.websites = [
"www.facebook.com",
"www.google.com",
"www.youtube.com",
"www.amazon.co.uk"
]
self.sites = random.choice(self.websites)
self.visit = "https://{}".format(self.sites)
wb.open(self.visit)
if __name__ == "__main__":
root = Tk()
root.title("Web Shortcut")
root.resizable(False, False)
root.geometry("500x200")
app = Browser(master=root)
app.mainloop()
root.destroy()
I have made some change, now it works very well
import webbrowser as wb
from tkinter import *
from tkinter import ttk
import tkinter as tk
import ctypes, random, time
class Browser(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.start()
def start(self):
global webinput
self.title = Label(self, text="Web Shortcut", fg="purple", font="Kokila 20 bold")
self.title.pack(fill=BOTH)
self.webinput = Entry(self)
self.webinput.pack(side=TOP, fill=BOTH, pady=30, padx=30)
self.openweb = Button(self, height=2, width=20, text="Open Web", bg="gray", fg="lightgreen", command=self.web)
self.openweb.pack(side=LEFT, padx=10)
self.random = Button(self, height=2, width=20, text="Random Web", bg="gray", fg="lightgreen", command=self.randomweb)
self.random.pack(side=LEFT, padx=5)
self.exit = Button(self, height=2, width=20, text="Exit", bg="gray", fg="lightgreen", command=exit)
self.exit.pack(side=LEFT, padx=5)
def web(self):
try:
self.sites = f"https://{self.webinput.get()}"
if self.sites:
wb.open(self.sites)
except Exception as e:
ctypes.windll.user32.MessageBoxW(0, str(e), "CRASHED", 0)
def randomweb(self):
self.websites = [
"www.facebook.com",
"www.google.com",
"www.youtube.com",
"www.amazon.co.uk"
]
self.sites = random.choice(self.websites)
self.visit = "https://{}".format(self.sites)
wb.open(self.visit)
if __name__ == "__main__":
root = Tk()
root.title("Web Shortcut")
root.resizable(False, False)
root.geometry("500x200")
app = Browser(master=root)
app.mainloop()
root.destroy()
Hope it helps. (●'◡'●)
I'm using ttk, for my GUI. I know that it is also a very simple question ... I am trying to change the background color of the main window.
I tried to change the theme, because I am working on a Mac, (and Python 3.5) to avoid the problem with the theme 'aqua', which is the default.I've been reading about several solutions like these questions which are about the same problem... These are the numbers of the questions:
54476511,
38712352,
47327266,
23750141.
But, I haven't Solve the problem, yet.
Here it's my code.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter.scrolledtext import *
from tkinter import Tk, BOTH, W, N, E, S, messagebox, END
from tkinter.ttk import Button, Label, Style, Frame
class Example(Frame):
def __init__(self,master):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Example")
Style().theme_use("classic")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
self.txt_Pad = ScrolledText(self)
self.txt_Pad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
self.txt_Pad.insert(END,'Type your info here')
btn_save = Button(self, text="Save", command=self.save_command)
btn_save.grid(row=1, column=3)
btn_close = Button(self, text="Close", command=self.onClose)
btn_close.grid(row=2, column=3, pady=4)
btn_help = Button(self, text="Help", command=self.about_command)
btn_help.grid(row=5, column=0, padx=5)
def onClose(self):
self.master.destroy()
def about_command(self):
msb = messagebox.showinfo("About", "\"Insert a useful tip Here\"")
def save_command(self):
print('Your info it\'s save now')
def open_command(self):
print('Choose your File')
def main():
root = Tk()
root.geometry("350x300+300+300")
root.configure(bg='#0059b3')
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
Any Suggestions would be appreciated.
Create a style then apply it.
from tkinter.scrolledtext import *
from tkinter import Tk, BOTH, W, N, E, S, messagebox, END
from tkinter.ttk import Button, Label, Style, Frame
class Example(Frame):
def __init__(self, master):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Example")
# create a new style
self.style = Style()
# configure it to the background you want
self.style.configure('My.TFrame', background='#0059b3')
#Style().theme_use("classic")
# apply it
self.config(style='My.TFrame')
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
self.txt_Pad = ScrolledText(self)
self.txt_Pad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
self.txt_Pad.insert(END,'Type your info here')
btn_save = Button(self, text="Save", command=self.save_command)
btn_save.grid(row=1, column=3)
btn_close = Button(self, text="Close", command=self.onClose)
btn_close.grid(row=2, column=3, pady=4)
btn_help = Button(self, text="Help", command=self.about_command)
btn_help.grid(row=5, column=0, padx=5)
def onClose(self):
self.master.destroy()
def about_command(self):
msb = messagebox.showinfo("About", "\"Insert a useful tip Here\"")
def save_command(self):
print('Your info it\'s save now')
def open_command(self):
print('Choose your File')
def main():
root = Tk()
root.geometry("350x300+300+300")
root.configure(background='#0059b3')
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
I left comments at the parts I changed.
Im just simply trying to print the values of the entry fields and it tells me its not defined. I been looking online for a long while now. The lastest thing that I tried was adding self as a parameter of add_student but that didn't work.
This is the code
from tkinter import *
class window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.widgets()
def add_student(self):
print(f"Student's name (first, last): {self.fname_textbox}, {self.lname_textbox}")
def widgets(self):
self.master.title("Student Test Score & Grade")
self.master.minsize(200,200)
""" Labels """
# First name
self.fname_label = Label(root, text='First Name: ')
self.fname_label.grid(row=0, column=0, padx=5, pady=5)
# Last Name
self.lname_label = Label(root, text='Last name: ')
self.lname_label.grid(row=1, column=0, padx=5, pady=5)
""" Entry boxes """
# First Name
self.fname_textbox = Entry(root, width=30)
self.fname_textbox.grid(row=0, column=1, padx=5)
# Last name
self.lname_textbox = Entry(root, width=30)
self.lname_textbox.grid(row=1, column=1, padx=5)
""" Buttons """
# Add Button
self.add_btn = Button(root, text="Add Student", command=self.add_student).grid(row=4, column=2, padx=2, pady=2)
if __name__=="__main__":
root = Tk()
root.resizable(width=False, height=False)
app = window(root)
root.mainloop()
It prints this
Student's name (first, last): .!entry, .!entry2
Instead of: Student's name (first, last): John, Doe
What does this mean?
You need to get the value of the entries in order to use them in your print statement
You do this by putting:
firstname = self.fname_textbox.get()
lastname = self.1fname_textbox.get()
before you print and using these values in your print statement.
Ah, Try something like this:
def __init__(self, master):
self.nameEntry = Entry(master)
self.contents = StringVar()
self.nameEntry["textvariable"]=self.contents
self.nameEntry.pack()
self.nameEntry.grid(row=0,column=1)
self.buttonSave = Button(master,text="Save",command=self.save).grid(row=9,columnspan=2, sticky=W+E+N+S)
def save(self):
print self.nameEntry.get()
Python novice, here. I've noticed there are a lot of questions around the topic of returning values from a TkInter function, but none of the solutions seem to solve my issue.
I can successfully print self.e1path to the shell from within getPath.submit, but I cannot return it to the rest of my code. I'm using a print statement outside of the class to test whether I've successfully returned the CSV path.
from tkinter import *
import tkinter as tk
class getPath(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label1 = tk.Label(self, text="CSV Path").grid(row=0, column=0)
self.e1 = tk.Entry(self, width=50)
self.e1Grid = self.e1.grid(row=0, column=1)
self.browse = tk.Button(self, text='Browse', command=self.getCSV).grid(row=0, column=2)
self.submit = tk.Button(self, text='Submit', command=self.submit).grid(row=1, column=1)
def getCSV(self):
self.fileName = filedialog.askopenfilename( filetypes = (('Comma Separated Values', '*.csv'), ('All Files', '*.*')), title = "Choose a CSV File")
self.e1.insert(10, self.fileName)
def submit(self):
self.e1Path = self.e1.get()
return self.e1Path
app = getPath()
app.mainloop()
print(app)
I figured it out! I needed to add a self.destroy() to the submit function. This stopped the mainloop and let me call on self.e1path outside of the function using app.e1path. New code:
from tkinter import *
import tkinter as tk
class getPath(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label1 = tk.Label(self, text="CSV Path").grid(row=0, column=0)
self.e1 = tk.Entry(self, width=50)
self.e1Grid = self.e1.grid(row=0, column=1)
self.browse = tk.Button(self, text='Browse', command=self.getCSV).grid(row=0, column=2)
self.submit = tk.Button(self, text='Submit', command=self.submit).grid(row=1, column=1)
def getCSV(self):
self.fileName = filedialog.askopenfilename( filetypes = (('Comma Separated Values', '*.csv'), ('All Files', '*.*')), title = "Choose a CSV File")
self.e1.insert(10, self.fileName)
def submit(self):
self.e1Path = self.e1.get()
self.destroy()
app = getPath()
app.mainloop()
print(app.e1Path)