Oct-13-2023, 05:20 PM
(This post was last modified: Oct-14-2023, 07:47 AM by deanhystad.)
Post entire error message, including trace.
This is your problem:
.grid(row=10, column = 5) returns None
When you daisychain the two together, Entry(root, text = "Message").grid(row=10, column = 5 returns None. All of the variables you assigned to widgets are None. myLabel1 == None, myButton3 == None, and, of course, e == None.
You don't use any of those variables, other than e, so don't assign the widget to a variable. For e, I think you should do something different. You can directly get or set text from an Entry widget, but I would use a tkinter StringVar. Like this:
This is your problem:
e = Entry(root, text = "Message").grid(row=10, column = 5)Entry(root, text = "Message") returns a tkEntry widget.
.grid(row=10, column = 5) returns None
When you daisychain the two together, Entry(root, text = "Message").grid(row=10, column = 5 returns None. All of the variables you assigned to widgets are None. myLabel1 == None, myButton3 == None, and, of course, e == None.
You don't use any of those variables, other than e, so don't assign the widget to a variable. For e, I think you should do something different. You can directly get or set text from an Entry widget, but I would use a tkinter StringVar. Like this:
import tkinter as tk # Do not use wildcard import bg = "black" fg = "white" # It is convention to put functions near top. def encrypt(): """Functions should have a docstring saying what they do.""" label.set("I clicked Encrypt!") # Instead of creating new labels, change the text of the existing label. def decrypt(): """I am called by the copy button?""" label.set(entry.get()) root = tk.Tk() root.configure(background=bg) entry = tk.StringVar(root, "Message") # Use this to get text from entry widget tk.Entry(root, textvariable=entry).grid(row=10, column=5) # Do not put multiple widgets in same grid location label = tk.StringVar(root, "") # Use this to set text of the label. tk.Label(root, textvariable=label, bg=bg, fg=fg).grid(row=0, column=1) # If you don't use a variable, don't create one. tk.Label(root, text="Hello World!", bg=bg, fg=fg).grid(row=0, column=0) tk.Label(root, text="My Name is Bob!", bg=bg, fg=fg).grid(row=1, column=1) tk.Button(root, text="Encrypt", command=encrypt, fg=fg, bg=bg).grid(row=2, column=1) tk.Button(root, text="Decrypt", state=tk.DISABLED, padx=50).grid(row=3, column=1) tk.Button(root, text="Copy", command=decrypt).grid(row=4, column=1) root.mainloop()This is the code without using StringVar. Setting the entry text becomes difficult. I also removed the fg and bg setting stuff. If you want to change the window appearance, use ttk widets and themes. I repurposed the copy button to randomly change the theme for the window when clicked.
import tkinter as tk # Do not use wildcard import from tkinter import ttk import random def encrypt(): label["text"] = "I clicked Encrypt!" def decrypt(): label["text"] = entry.get() def change_theme(): theme = random.choice(style.theme_names()) label["text"] = theme style.theme_use(theme) root = tk.Tk() style = ttk.Style(root) entry = ttk.Entry(root) entry.insert(0, "Message") # This is why you use a StringVar. entry.grid(row=10, column=5) label = ttk.Label(root, text="") label.grid(row=0, column=1) # If you don't use a variable, don't create one. ttk.Label(root, text="Hello World!").grid(row=0, column=0) ttk.Label(root, text="My Name is Bob!").grid(row=1, column=1) ttk.Button(root, text="Encrypt", command=encrypt).grid(row=2, column=1) ttk.Button(root, text="Decrypt", command=decrypt).grid(row=3, column=1) ttk.Button(root, text="Copy", command=change_theme).grid(row=4, column=1) root.mainloop()