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()
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
The program is made up of classes and I am trying to use a tkinter topview from within a function so that when it's called it is able to retrieve the entryfield value to the master class
from tkinter import
from PIL import Image, ImageTk
Below is the driver code handling the transitioning from one class to another
class SeaofBTCapp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (
WelcomePage, Register_new_user): # ,PageThree,PageFour):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(WelcomePage)
# def show_frame(self, cont):
# frame = self.frames[cont]
# frame.tkraise()
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
frame.update()
frame.event_generate("<<show_frame>>")
def get_page(self, cont):
for page in self.frames.values():
if str(page.__class__.__name__) == cont:
return page
return None
class Register_new_user(object):
pass
Below is the entry point of the program and is the first page to be displayed
class WelcomePage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
# self.bind("<<show_frame>>", self.main_prog)
def resize_image(event):
global photo
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image=photo)
label.image = photo # avoid garbage collection
def pin_input():
top = Toplevel()
top.geometry("180x100")
top.title("toplevel")
l2 = Label(top, text="This is toplevel window")
global entry_1
global password
password = StringVar
entry_1 = None
def cleartxtfield():
global password
new = "3"
password.set(new)
# ############# Function to parse for only numerical input
def validate(input):
if input.isdigit():
return True
elif input == "":
return True
else:
return False
def enternumber(x):
global entry_1
setval = StringVar()
setval = str(x)
# print(setval)
entry_1.insert(END, setval)
entry_1 = Entry(top, textvariable=password, width=64, show='*')
entry_1.place(x=200, y=100)
entry_1.focus()
reg = top.register(validate)
entry_1.config(validate="key", validatecommand=(reg, '%P'))
def getcreds():
# check if four digit entered and is not empty
global passwd
passwd = password.get()
print(f"The Credentials are {passwd}")
def funcbackspace():
length = len(entry_1.get())
entry_1.delete(length - 1, 'end')
def killwindow():
# when the user quits it should clear all the data input fields filled in in the previous steps. and should display information that it is about to quit in a few seconds
command = top.destroy()
# Label(top,text="Goodbye\n (Closing in 2 seconds)")
top.after(2000, top.quit())
cancel = Button(top, width=8, height=3, text="Cancel", bg="red", fg="black", command=killwindow)
cancel.place(x=220, y=150)
backspace = Button(top, width=8, height=3, text="Backspace", bg="red", fg="black", command=funcbackspace)
backspace.place(x=500, y=150)
# ----number Buttons------
def enternumber(x):
global entry_1
setval = StringVar()
setval = str(x)
# print(setval)
entry_1.insert(END, setval)
btn_numbers = []
for i in range(10):
btn_numbers.append(
Button(top, width=8, height=3, text=str(i), bd=6, command=lambda x=i: enternumber(x)))
btn_text = 1
for i in range(0, 3):
for j in range(0, 3):
btn_numbers[btn_text].place(x=220 + j * 140, y=250 + i * 100)
btn_text += 1
btn_zero = Button(top, width=15, height=2, text='0', bd=5, command=lambda x=0: enternumber(x))
btn_zero.place(x=330, y=550)
clear = Button(top, text="Clear", bg="green", fg="white", width=8, height=3, command=cleartxtfield)
clear.place(x=220, y=550)
okbtn = Button(top, text="Enter", bg="green", fg="black", width=8, height=3, command=getcreds)
okbtn.place(x=500, y=550)
val = getcreds()
print("The value to be returned is %s" % val)
return val
password = pin_input()
print("Gotten password is %s" % password)
copy_of_image = Image.open("image.png")
photoimage = ImageTk.PhotoImage(copy_of_image)
label = Label(self, image=photoimage)
label.place(x=0, y=0, relwidth=1, relheight=1)
label.bind('<Configure>', resize_image)
top_left_frame = Frame(self, relief='groove', borderwidth=2)
top_left_frame.place(relx=1, rely=0.1, anchor=NE)
center_frame = Frame(self, relief='raised', borderwidth=2)
center_frame.place(relx=0.5, rely=0.75, anchor=CENTER)
Button(top_left_frame, text='REGISTER', bg='grey', width=14, height=1,
command=lambda: controller.show_frame(Register_new_user)).pack()
Button(center_frame, text='ENTER', fg='white', bg='green', width=13, height=2,
command=lambda: controller.show_frame(Register_new_user)).pack()
if __name__ == '__main__':
app = SeaofBTCapp()
app.title("Password return on topview window")
width = 1000
height = 700
screenwidth = app.winfo_screenwidth()
screenheight = app.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
app.geometry(alignstr)
# app.resizable(width=False, height=False)
app.resizable(width=True, height=True)
app.mainloop()
If I understand this correctly you want to enter a password in a dialog and then get the password from the dialog when you close it.
Have a look at Dialog Windows at effbot for a discussion about creating dialog windows.
Here is an example of how you can implement a simple dialog:
from tkinter import *
from tkinter import simpledialog
class MyDialog(simpledialog.Dialog):
def body(self, master):
'''create dialog body.
return widget that should have initial focus.
This method should be overridden, and is called
by the __init__ method.'''
Label(master, text='Value:').grid(row=0)
self.e1 = Entry(master)
self.e1.grid(row=0, column=1)
return self.e1 # initial focus
def apply(self):
'''process the data
This method is called automatically to process the data, *after*
the dialog is destroyed. By default, it does nothing.'''
value = self.e1.get()
self.result = value
def validate(self):
'''validate the data
This method is called automatically to validate the data before the
dialog is destroyed. By default, it always validates OK.'''
return 1 # override
def buttonbox(self):
'''add standard button box.
override if you do not want the standard buttons
'''
box = Frame(self)
w = Button(box, text="OK", width=10, command=self.ok, default='active')
w.pack(side='left', padx=5, pady=5)
w = Button(box, text="Cancel", width=10, command=self.cancel)
w.pack(side='left', padx=5, pady=5)
self.bind("<Return>", self.ok)
self.bind("<Escape>", self.cancel)
box.pack()
if __name__ == '__main__':
root = Tk()
root.geometry('200x100+800+50')
def do():
d = MyDialog(root)
print(d.result)
b = Button(root, text='Go!', width=10, command=do)
b.pack(expand=True)
Did that answer your question?
I am still relatively new at Python, but I am making a GUI app that has 2 entry fields and two filedialog buttons for the user to select the file to import and the directory to save the output of the program. I am trying to do some validation on the entry fields to make sure that the user cannot click on the submit button until the entry fields are filled in and they have selected a file to import and a directory to save the output.
I got some of the way, but I'm stuck and I'm afraid I don't know enough about classes and methods to determine why I cannot change the status of my submit_button.config?
I have read various examples of how to do validation to entry fields including using validatecommand and building a validate method within my class. I abandoned that because I could not figure out how to validate multiple fields within the submit_button command.
Here is my code as it sits right now. I am struggling with the validate method within the Application class.
import pandas as pd
import numpy as np
from tkinter import *
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter import messagebox
import os
class FileLogic:
def __init__(self, path, save_location, request_id, exeuction_id):
self.path = path
self.save_location = save_location
self.request_id = request_id
self.execution_id = execution_id
def fileopen(self=None):
global fileName
global path
path = fd.askopenfilename(title = "Select File", filetypes=( ("Excel files", "*.xlsx"),("All files", "*.*") ) )
fileName = os.path.split(path)[1]
if not fileName:
messagebox.showerror("ERROR - File Not Selected", "A file was not selected to process. Please select a file by double-clicking or select file and press Open button")
else:
file_select_label = Label(root, text=("File Selected: " + fileName), width=75, bg="light blue")
file_select_label.grid(row=7, columnspan=2)
return path
def filesave(self=None):
global save_location
save_location = fd.askdirectory(title = "Select Directory")
if not save_location:
messagebox.showerror("ERROR - Directory Not Selected", "This upload process will build an output file. Please select a folder where the output file can be saved")
else:
file_select_label = Label(root, text=("Output file will be saved: " + save_location), width=75, bg="light blue")
file_select_label.grid(row=8, columnspan=2)
return save_location
def submit(self, path, save_location, request_id, execution_id):
print("FileLogic path: " + self.path)
print("FileLogic save: " + self.save_location)
print("FileLogic request: " + self.request_id)
print("FileLogic execution: " + self.execution_id)
# FileParsing.__init__(request_id)
class FileParsing:
def __init__(self, request_id):
self.request_id = request_id
# self.execution_id_entry = execution_id_entry
print("request id2: " + request_id)
class Application(Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
global submit_button
##### Define the Labels ###############
self.request_id_label = Label(root, text="Enter Rebate Request Id:", bg="light blue", bd=2, width=25).grid(row=0, column=0)
self.execution_id_label = Label(root, text="Enter Rebate Execution Id:", bg="light blue", bd=2, width=25).grid(row=1, column=0)
self.blank_label = Label(root, bg="light blue")
####### Define the Entry fields ##################
self.request_id_entry = Entry(root,bg="light gray", bd=2, width=25, textvariable=request_id_entry).grid(row=0, column=1)
self.execution_id_entry = Entry(root, bg="light gray", bd=2, width=25, textvariable=execution_id_entry).grid(row=1, column=1)
###### Define the Buttons ###############
self.submit_button = Button(root, text="Submit", bg="gray", width=17, command= lambda: self.submit_click(path, save_location, request_id, execution_id))
self.submit_button.config(state='disabled')
self.open_file_button = Button(root, text="Select file to process", width = 30, command=FileLogic.fileopen).grid(row=3, column=0)
self.save_location_button = Button(root, text="Select location to save output", width=30, command=FileLogic.filesave).grid(row=4, column=0)
##### Build the Grid ##################
self.blank_label.grid(row=2, column=0)
self.blank_label.grid(row=5, columnspan=2)
self.submit_button.grid(row=6, column=1)
def validate(self, *args):
print("validate")
button_status = self.create_widgets(submit_button)
if request_id_entry.get():
print("normal")
print(button_status)
# self.submit_button.config(state='normal')
else:
print("diabled")
print(submit_button.config)
# self.submit_button.config(state='disabled')
def num_check(self,var):
var = self.var.get()
print(var)
if var.isnumeric():
return True
else:
tkinter.messagebox.showinfo("Error", "Enter Numeric Value")
def submit_click(self, path, save_location, request_id, execution_id):
self.request_id = request_id_entry.get()
self.execution_id = execution_id_entry.get()
a = FileLogic(path, save_location, request_id, execution_id)
FileLogic.submit(a, path, save_location, request_id, execution_id)
root=Tk()
root.title("Rebate Bid Data Upload")
root.geometry("500x200")
root.configure(background="light blue")
request_id_entry = StringVar()
execution_id_entry = StringVar()
request_id_entry.trace("w", Application.validate)
app = Application(root)
root.mainloop()
I am trying to get where the submit button is disabled until all the entry elements and filedialog attributes are complete. Then for the entry fields I am checking to make sure they are numeric and I will want to make sure they are integers.
You are not using textvariable correctly. Also note that you are not keeping a reference of your entry widgets by defining them and calling the grid method on the same line.
def create_widgets(self):
#global submit_button #you don't have to declare global here: submit_button is already an attribute
...
self.request_var = StringVar() #create StringVars for request
self.execution_var = StringVar() #ditto for execution
self.request_id_entry = Entry(root,bg="light gray", bd=2, width=25,textvariable=self.request_var).grid(row=0, column=1) #set the textvariable to the StringVar
self.execution_id_entry = Entry(root, bg="light gray", bd=2, width=25,textvariable=self.execution_var).grid(row=1, column=1)
self.request_var.trace("w",self.validate) #trace changes on StringVar
self.execution_var.trace("w",self.validate)
...
def validate(self, *args):
if self.request_var.get() and self.execution_var.get(): #if both StringVars has content
print("normal")
self.submit_button.config(state='normal')
else:
print("disabled")
self.submit_button.config(state='disabled')
import tkinter as tk
import os
import tkinter.messagebox as tm
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(StartPage)
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
class StartPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="Please select an option below").pack(side="top", fill="x", pady=10)
tk.Button(self, text="Register",
command=lambda: master.switch_frame(register_screen)).pack()
tk.Button(self, text="Login",
command=lambda: master.switch_frame(login_screen)).pack()
class register_screen(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
global username
global password
global username_entry
global password_entry
self.username = StringVar()
self.password = StringVar()
Label(self, text="Please enter details below").pack()
Label(self, text="").pack()
username_lable = Label(self, text="Username * ")
username_lable.pack()
username_entry = Entry(self, textvariable=self.username)
username_entry.pack()
password_lable = Label(self, text="Password * ")
password_lable.pack()
password_entry = Entry(self, textvariable=self.password, show='*')
password_entry.pack()
Label(self, text="").pack()
Button(self, text="Register", width=10, height=1, command = register_user).pack()
class login_screen(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="Login Page").grid(row=0)
global username_verify
global password_verify
self.username_verify = StringVar()
self.password_verify = StringVar()
global username_entry1
global password_entry1
tk.Label(self, text = "Username").grid(row = 3, sticky=E)
username_entry1 = Entry(self,textvariable = username_verify)
username_entry1.grid(row = 3, column = 1)
tk.Label(self, text = "Password").grid(row = 6, sticky=E)
password_entry1 = Entry(self,show="*",textvariable = password_verify)
password_entry1.grid(row = 6, column = 1)
tk.Checkbutton(self, text = "Keep Me Logged In").grid(columnspan = 2)
tk.Button(self,text= "Login", command= login_verify).grid(row = 8)
Got Error: register_user() missing 1 required positional argument: 'self' (or) login_verify() missing 1 required positional argument: 'self'.
I am not understanding what I am missing here. I am new to python so, please explain me using references for my further understanding.Please help me out.if there are any mistakes in the rest of the code or any scope for improvement, please provide the suggestions.
def register_user(self):
username_info = self.username.get()
password_info = self.password.get()
file = open(r"C:\Users\ashita.gadagotti\Desktop\username_info.txt", "w+")
file.write(username_info + "\n")
file.write(password_info)
file.close()
username_entry.delete(0, END)
password_entry.delete(0, END)
Label(self, text="Registration Success.Please log in with the new credentials.", fg="green", font=("calibri", 11),command = lambda: master.switch_frame(login_page)).pack()
def login_verify(self):
username1 = self.username_entry1.get()
password1 = self.password_entry1.get()
username_entry1.delete(0, END)
password_entry1.delete(0, END)
logindetails = os.listdir(r"C:\Users\ashita.gadagotti\Desktop\username_info.txt")
if username1 == logindetails:
file1 = open(username1, "r")
verify = file1.read().splitlines()
if password1 in verify:
master.switch_frame(SearchPage)
else:
tm.showerror("Login error","password has not been recognized")
else:
tm.showerror("Login error","User not found!")
class searchpage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="SearchPage").grid(row = 0)
tk.Entry().grid(row = 1)
tk.Button(self, text="Search")
if __name__ == "__main__":
app = SampleApp()
app.title("Equiniti Search Engine")
app.geometry('1280x720')
app.mainloop()
self is a reserved keyword in python, meaning that it has a special meaning in the language. See the answers to this question to learn what it means.In general, it is used as a first argument to methods of class in order to access the reference to the object in question.
From your code, I can see two issues
Indent switch_frame method and its body so that it is recognized as a method of the class
register_user and login_verify seems to be using references to attributes of a class so you need to put them under login_screen class definition
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.