Tkinter global variable/.get() Tkinter global variable/.get() tkinter tkinter

Tkinter global variable/.get()


PyCharm wants you to follow a specific way of writing code. What it is telling you is that you have not defined sender_email in the global namespace first. By adding this variable to the global namespace and giving it a default value PyCharm will get rid of the "warning".

from tkinter import *root = Tk()root.title("E-mail Application")sender_emaillabel= Label(root,text="Enter Sender Email:").grid(row=0, column=0,columnspan=1)e = Entry(root, width=35, borderwidth=5)e.grid(row=1, column=0, columnspan=3, padx=10, pady=10)sender_email = ''  # Define it somewhere before the function.                   # This will get rid of the lines under your code and the warning.def entersender_click():    global sender_email    sender_email=e.get()    e.delete(0, END)    returnentersender= Button(root, text="Enter", padx=10, pady=5, command=entersender_click).grid(row=1, column=4)root.mainloop()

I personally find this warning annoying myself as I get it a lot in my classes as I do not define all of the class attributes in the __init__ like it wants you to.

A few things to change thought.

  1. Use import tkinter as tk instead of import *

  2. There is no need to define your widget by name if you are applying the geometry manager directly to them and if you do not intend to make changes to them down the road.

  3. You have no reason to use return in your function so you can remove that.

Updated Code:

import tkinter as tkroot = tk.Tk()root.title("E-mail Application")tk.Label(root,text="Enter Sender Email:").grid(row=0, column=0,columnspan=1)e = tk.Entry(root, width=35, borderwidth=5)e.grid(row=1, column=0, columnspan=3, padx=10, pady=10)sender_email = ''def entersender_click():    global sender_email    sender_email=e.get()    e.delete(0, END)tk.Button(root, text="Enter", padx=10, pady=5, command=entersender_click).grid(row=1, column=4)root.mainloop()