I am trying to get the text username and password in the below Tkinter to be next to the Entry, I have tried to play with the columns but it didn't fix it. I was previously using pack() but switched to grid() to be more in control of the location of the labels.
Here is the code:
root = Tk()
root.title("Bookmark Search")
root.resizable(0,0)
greeting= Label(root, text="Hi, This is a Trial to see how the label works !")
help=Label(root, text=" How can I help you today?")
greeting.grid(row = 0, column= 1, pady=5, padx=200)
help.grid(row = 1, column= 1,pady=5)
e = Entry(root, width=50)
e.grid(row=2, column = 1,rowspan=2, pady=15)
mySubmit = Label(root)
-------several lines of unrelated code-------
mySubmit.bind("<Button-1>", open_url)
root.bind('<Return>', myClick)
myButton= Button(root, text="Submit", command=myClick)
myButton.grid(row=4, column = 1,rowspan=2, pady=10)
# username
username_label = Label(root, text="Username:")
username_label.grid(column=0, row=8, padx=5, pady=5)
username_entry = Entry(root)
username_entry.grid(column=1, row=8, padx=5, pady=5)
# password
password_label = Label(root, text="Password:")
password_label.grid(column=0, row=9, padx=5, pady=5)
password_entry = Entry(root)
password_entry.grid(column=1, row=9, padx=5, pady=5)
# login button
login_button = Button(root, text="Login")
login_button.grid(column=1, row=10,padx=5, pady=5)
root.mainloop()
Here is a print screen of the current output:
Here is the required output:
You can put Labels and Buttons in Frame and then Frame put in main window.
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions --- (PEP8: lower_case_names)
def open_url():
pass
def my_click():
pass
# --- main ---
root = tk.Tk()
root.title("Bookmark Search")
root.resizable(0,0)
greeting = tk.Label(root, text="Hi, This is a Trial to see how the label works !")
help = tk.Label(root, text=" How can I help you today?")
greeting.grid(row=0, column=1, pady=5, padx=200)
help.grid(row=1, column=1, pady=5)
e = tk.Entry(root, width=50)
e.grid(row=2, column=1, pady=15)
my_submit = tk.Label(root, text='LABEL')
my_submit.grid(row=3, column=1, pady=10)
my_submit.bind("<Button-1>", open_url)
root.bind('<Return>', my_click)
my_button = tk.Button(root, text="Submit", command=my_click)
my_button.grid(row=4, column=1, pady=10)
# - start Frame - to group widgets -
frame = tk.Frame(root)
frame.grid(column=1, row=5)
# username
username_label = tk.Label(frame, text="Username:")
username_label.grid(column=0, row=8, padx=5, pady=5)
username_entry = tk.Entry(frame)
username_entry.grid(column=1, row=8, padx=5, pady=5)
# password
password_label = tk.Label(frame, text="Password:")
password_label.grid(column=0, row=9, padx=5, pady=5)
password_entry = tk.Entry(frame)
password_entry.grid(column=1, row=9, padx=5, pady=5)
# - end Frame -
# login button
login_button = tk.Button(root, text="Login")
login_button.grid(column=1, row=10, padx=5, pady=5)
root.mainloop()
PEP 8 -- Style Guide for Python Code
Related
there are some space between the result as you can see in the image that password entry and generate password aren't aligning together
*Result image
from tkinter import
window = Tk()
window.title("Password Manager")
window.config(padx=20, pady=20)
canvas = Canvas(width=200, height=200, highlightthickness=0)
password_image = PhotoImage(file="logo.png")
image = canvas.create_image(100, 100, image=password_image)
canvas.itemconfig(image)
canvas.grid(row=0, column=1)
website_label = Label(text="Website")
website_label.grid(row=1, column=0)
Email_username_label = Label(text="Email/Username")
Email_username_label.grid(row=2, column=0)
password_label = Label(text="Password")
password_label.grid(row=3, column=0)
website_input = Entry(width=35)
website_input.grid(row=1, column=1, columnspan=2)
Email_username_input = Entry(width=35)
Email_username_input.grid(row=2,column=1, columnspan=2)
password = Entry(width=21)
password.grid(row=3, column=1)
generate_button = Button(text="Generate Password")
generate_button.grid(row=3, column=2)
add_password_button = Button(text="Add", width=36)
add_password_button.grid(row=4, column=1, columnspan=2)
window.mainloop()
You can see that there is some space in password entry and generate password how to fix it
As noted in the comments, using the grid method allows you to just add sticky to align elements. In this case, I've removed all the width options in your entries and buttons with stick='EW' to expand East and West, or to the maximum left and right of the column the widget is in.
from tkinter import *
window = Tk()
window.title("Password Manager")
window.config(padx=20, pady=20)
# Canvas
canvas = Canvas(width=200, height=200, highlightthickness=0)
password_image = PhotoImage(file="logo.png")
image = canvas.create_image(100, 100, image=password_image)
canvas.itemconfig(image)
canvas.grid(row=0, column=1)
# Labels
website_label = Label(text="Website")
website_label.grid(row=1, column=0)
Email_username_label = Label(text="Email/Username")
Email_username_label.grid(row=2, column=0)
password_label = Label(text="Password")
password_label.grid(row=3, column=0)
# Entries
website_input = Entry()
website_input.grid(row=1, column=1, columnspan=2, sticky='EW') # sticky
Email_username_input = Entry()
Email_username_input.grid(row=2, column=1, columnspan=2, sticky='EW') # sticky
password = Entry()
password.grid(row=3, column=1, sticky='EW') # sticky
# Buttons
generate_button = Button(text="Generate Password")
generate_button.grid(row=3, column=2)
add_password_button = Button(text="Add")
add_password_button.grid(row=4, column=1, columnspan=2, sticky='EW') # sticky
window.mainloop()
You may also want to consider reading up on column and row weights using columnconfigure and rowconfigure if you want your GUI to resize dynamically.
This question already has answers here:
tkinter creating buttons in for loop passing command arguments
(3 answers)
Closed 6 months ago.
I'm trying to program an interface with tkinter, that shows users of my application as a list of Labels. The list is created from a database (created with sqlite3).
By clicking on each Label the program should open another window, showing details of the clicked user.
maybe do I need to use classes? (because for now, my script is not OOP)
root = Tk()
root.title("Utenti")
root.iconbitmap("D:\Willow\WilloW SW Gestionale")
root.geometry("365x600")
def open_user(name = str):
user = Tk()
user.title("Utenti")
user.iconbitmap("D:\Willow\WilloW SW Gestionale")
user.geometry("500x400")
#create entry
f_name_entry = Entry(user, width=30, state="disabled")
f_name_entry.grid(row=0, column=1, padx=20, pady=(10, 0))
P_iva_entry = Entry(user, width=30, state="disabled")
P_iva_entry.grid(row=1, column=1)
cell_number_entry = Entry(user, width=30, state="disabled")
cell_number_entry.grid(row=2, column=1)
tax_entry = Entry(user, width=30, state="disabled")
tax_entry.grid(row=3, column=1)
inps_entry = Entry(user, width=30, state="disabled")
inps_entry.grid(row=4, column=1)
# create text boxes
f_name_label = Label(user, text=name)
f_name_label.grid(row=0, column=0, pady=(10, 0))
P_iva_label = Label(user, text="p iva")
P_iva_label.grid(row=1, column=0)
cell_number_label = Label(user, text="cell number")
cell_number_label.grid(row=2, column=0)
tax_label = Label(user, text="tax")
tax_label.grid(row=3, column=0)
inps_label = Label(user, text="inps")
inps_label.grid(row=4, column=0)
i = 4
conn = sqlite3.connect("usersEsteso.db")
c = conn.cursor()
c.execute("SELECT *, oid FROM usersEsteso")
records = c.fetchall()
for record in records:
print_records = record[0]
users_lable = Label(root, bg="#778899")
users_lable.grid(row=i + 7, column=0, sticky="w", padx=5, pady=5, columnspan=3)
user_text = Label(users_lable, text=print_records)
user_text.grid(row=0, column=0, pady=5, padx=5, sticky="w")
#user_text.bind("<Enter>", open_user("sam"))
openUser_lable_button = Button(users_lable, text="apri " + record[0], command=lambda: open_user(record[0])) #here i showld pass the identification of the parent the button
openUser_lable_button.grid(row=1, column=0, pady=5, padx=5, sticky="e")
i = i + 1
conn.commit()
conn.close()
here the previous code, without database and simplyfied:
from tkinter import *
root = Tk()
root.title("Users")
root.geometry("365x600")
#global variables
i = 1
def open_user():
user = Tk()
user.title("User")
user.geometry("500x400")
#create entry
f_name_entry = Entry(user, width=150, state="normal")
f_name_entry.grid(row=0, column=1, padx=20, pady=(10, 0))
P_iva_entry = Entry(user, width=150, state="disabled")
P_iva_entry.grid(row=1, column=1, columnspan=2)
cell_number_entry = Entry(user, width=150, state="disabled")
cell_number_entry.grid(row=2, column=1, columnspan=2)
tax_entry = Entry(user, width=150, state="disabled")
tax_entry.grid(row=3, column=1, columnspan=2)
inps_entry = Entry(user, width=150, state="disabled")
inps_entry.grid(row=4, column=1, columnspan=2)
# create text boxes
f_name_label = Label(user, text="name")
f_name_label.grid(row=0, column=0, pady=(10, 0))
P_iva_label = Label(user, text="p iva")
P_iva_label.grid(row=1, column=0)
cell_number_label = Label(user, text="cell number")
cell_number_label.grid(row=2, column=0)
tax_label = Label(user, text="tax")
tax_label.grid(row=3, column=0)
inps_label = Label(user, text="inps")
inps_label.grid(row=4, column=0)
f_name_entry.insert(0, "here should be the name of the clicked user")
my_list = ["andrew", "sam", "Zoe"]
title_label = Label(root, text="List of Users")
title_label.grid(row=0, column=0, pady=(10,40))
for item in my_list:
print(i)
my_frame = Frame(root, bg="#a6a6a6")
my_label = Label(my_frame, text=item)
my_button = Button(my_frame, text="inspect user", command=open_user)
my_frame.grid(row=i, column=0, padx=10, pady=10)
my_label.grid(row=0, column=0, padx=10, pady=10)
my_button.grid(row=1, column=0, padx=10, pady=10)
i = i+1
root.mainloop()
I have just started learning tkinter and am having troubles with moving items around using grid. I am assuming this is an easy fix but I am trying to get my password entry and "generate password" button to be located much closer than they are as seen in the attached image (essentially I want everything aligned). How can I do this? I have looked on here and elsewhere but can't seem to find anything that replicates my problem.
from tkinter import *
window = Tk()
window.title("Password Manager")
window.config(padx=50, pady=50)
canvas = Canvas(height=200, width=200)
canvas.grid(column=1, row=0)
lock = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=lock)
website = Label(text="Website:")
website.grid(column=0, row=1)
email_user_name = Label(text="Email/Username:")
email_user_name.grid(column=0, row=2)
password = Label(text="Password:")
password.grid(column=0, row=3)
website_entry = Entry(width=35)
website_entry.grid(column=1, row=1, columnspan=2)
email_entry = Entry(width=35)
email_entry.grid(column=1, row=2, columnspan=2)
password_entry = Entry(width=21)
password_entry.grid(column=1, row=3)
generate_button = Button(text="Generate Password")
generate_button.grid(column=2, row=3)
add_button = Button(text="Add", width=30)
add_button.grid(column=1, row=4, columnspan=2)
window.mainloop()
You can add sticky="e" to grid(...) on the labels and sticky="w" on the entries:
from tkinter import *
window = Tk()
window.title("Password Manager")
window.config(padx=50, pady=50)
canvas = Canvas(height=200, width=200)
canvas.grid(column=1, row=0)
lock = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=lock)
website = Label(text="Website:")
website.grid(column=0, row=1, sticky="e")
email_user_name = Label(text="Email/Username:")
email_user_name.grid(column=0, row=2, sticky="e")
password = Label(text="Password:")
password.grid(column=0, row=3, sticky="e")
website_entry = Entry(width=35)
website_entry.grid(column=1, row=1, columnspan=2, sticky="w")
email_entry = Entry(width=35)
email_entry.grid(column=1, row=2, columnspan=2, sticky="w")
password_entry = Entry(width=21)
password_entry.grid(column=1, row=3, columnspan=2, sticky="w")
generate_button = Button(text="Generate Password")
generate_button.grid(column=3, row=3, sticky="w")
add_button = Button(text="Add", width=30)
add_button.grid(column=1, row=4, columnspan=2)
window.mainloop()
Note that I have added columnspan=2 to password_entry.grid(...) and moved generate_button to column 3.
I want to have a GUI with 2 buttons. Once clicked on either button, I want to see a new GUI which has a button to go back to the main GUI with the two buttons again.
This is what I've got right now but the 'Go back' button doesn't do anything. How can I go back to my first page using tkinter?
from tkinter import *
root = Tk()
root.title('Frames')
root.geometry('500x250+300+300')
# Position frame
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
# What do the buttons do
def bad():
frame.grid_forget()
b.grid_forget()
b2.grid_forget()
slechtekeuze = Label(frame, text='Bad choice')
slechtekeuze.grid(row=0, column=0, columnspan=2)
# Option to got back
homepage = Button(frame, text='Go back', command=back)
homepage.grid(row=1, column=0, columnspan=2, pady=10)
def good():
frame.grid_forget()
b.grid_forget()
b2.grid_forget()
slechtekeuze = Label(frame, text='Good choice')
slechtekeuze.grid(row=0, column=0, columnspan=2)
# Option to go back
homepage = Button(frame, text='Terug', command=back)
homepage.grid(row=1, column=0, columnspan=2, pady=10)
def back():
frame.grid_forget()
frame1 = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame1.pack(padx=10, pady=50)
b = Button(frame1, text="Don't click!!!", fg='red', command=bad)
b2 = Button(frame1, text='Click!!!', fg='green', command=good)
b.grid(row=0, column=0, padx=3)
b2.grid(row=0, column=1, padx=3)
# Create the buttons and put them in the frame
b = Button(frame, text="Don't click!!!", fg='red', command=bad)
b2 = Button(frame, text='Click!!!', fg='green', command=good)
b.grid(row=0, column=0, padx=3)
b2.grid(row=0, column=1, padx=3)
root.mainloop()
It does work, expand your screen a bit. The frames get added below.
I've edited it a bit as much in your style as possible:
from tkinter import *
root = Tk()
root.title('Frames')
root.geometry('500x250+300+300')
# Position frame
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
# What do the buttons do
def bad(frame):
frame.destroy()
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
slechtekeuze = Label(frame, text='Bad choice')
slechtekeuze.grid(row=0, column=0, columnspan=2)
# Option to got back
homepage = Button(frame, text='Go back', command=lambda:back(frame))
homepage.grid(row=1, column=0, columnspan=2, pady=10)
def good(frame):
frame.destroy()
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
slechtekeuze = Label(frame, text='Good choice')
slechtekeuze.grid(row=0, column=0, columnspan=2)
# Option to go back
homepage = Button(frame, text='Terug', command=lambda:back(frame))
homepage.grid(row=1, column=0, columnspan=2, pady=10)
def back(frame):
frame.destroy()
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
b = Button(frame, text="Don't click!!!", fg='red', command=lambda:bad(frame))
b2 = Button(frame, text='Click!!!', fg='green', command=lambda:good(frame))
b.grid(row=0, column=0, padx=3)
b2.grid(row=0, column=1, padx=3)
# Create the buttons and put them in the frame
b = Button(frame, text="Don't click!!!", fg='red', command=lambda:bad(frame))
b2 = Button(frame, text='Click!!!', fg='green', command=lambda:good(frame))
b.grid(row=0, column=0, padx=3)
b2.grid(row=0, column=1, padx=3)
root.mainloop()
I have a simple label and entry field that would:
1) Create a static label and clear the entry field after confirmation button click
2) Clear static label after reset button click
Is there any way to overwrite the entry field with a static label of the user input on the confirmation click instead of creating a new static label? And overwriting the static label with an empty entry field on the reset click?
Thank you for the help in advance.
from tkinter import *
root = Tk()
frame1 = Frame(root)
frame1.pack()
def reset():
set_cname.destroy()
cbtn['state'] = NORMAL
def confirm():
global set_cname
text1="Customer Name: " + entry1.get()
set_cname = Label(frame1, text=text1)
set_cname.grid(row=3, column=0, columnspan=1)
entry1.delete(0, 'end')
cbtn['state'] = DISABLED
cname = Label(frame1, text="Customer Name: ").grid(padx=5, pady=5, columnspan=2, sticky=W)
entry1 = Entry(frame1)
entry1.grid(row=0, column=2, padx=5)
cbtn = Button(frame1, text="Confirm", command=confirm, width=20)
cbtn.grid(row=1, column=4, padx=5, pady=5)
rbtn = Button(frame1, text="Reset Names", command=reset, width=20)
rbtn.grid(row=2, column=4, padx=5, pady=5)
root.mainloop()
You can replace the Entry with a Label by first creating both and then using pack() to switch between them. The trick is to not let their different sizes affect the application layout, which can be accomplished by disabling size propagation.
In my example I create a new frame (entry_frame) with a fixed size and then disable size propagation (.pack_propagate(False)). Then I use this new frame to contain the Entry/Label. Im giving the entry_frame the bg color khaki to let you see exactly where it is.
I fiddled a bit with the column numbers also.
from tkinter import *
root = Tk()
frame1 = Frame(root)
frame1.pack()
def reset():
text_label.pack_forget()
entry1.pack()
cbtn['state'] = NORMAL
def confirm():
global set_cname
entry1.pack_forget()
text_label.config(text=entry1.get())
text_label.pack(side='left')
entry1.delete(0, 'end')
cbtn['state'] = DISABLED
cname = Label(frame1, text="Customer Name: ")
cname.grid(row=0, column=0, padx=5, pady=5, sticky=W)
entry_frame = Frame(frame1, width=130, height=20, bg='khaki')
entry_frame.grid(row=0, column=1, padx=5, pady=5, sticky='nsew')
entry_frame.pack_propagate(False) # Disable size propagation
entry1 = Entry(entry_frame) # Customer name entry
entry1.pack()
text_label = Label(entry_frame) # Label to hold customer name
cbtn = Button(frame1, text="Confirm", command=confirm, width=20)
cbtn.grid(row=1, column=2, padx=5, pady=5)
rbtn = Button(frame1, text="Reset Names", command=reset, width=20)
rbtn.grid(row=2, column=2, padx=5, pady=5)
root.mainloop()
Be aware that this solution will be sensitive to font size changes.