Home > Software engineering >  How do I make my program Print the H variable after Hello [duplicate]
How do I make my program Print the H variable after Hello [duplicate]

Time:09-28

from tkinter import *
root = Tk()

root.title("Matchstick Game")
root.iconbitmap("match.ico")
root.geometry("1000x580")

H = Entry(root, width = 80, fg = 'red').pack()
def clickme():
mylabel = Label(root, text = "Hello"   H.get()).pack()

my_button = Button(root, text = "whats your name", 
padx=10,pady=10,bg='white',fg='green',command=clickme).pack()

root.mainloop()

I am getting this error: AttributeError: 'NoneType' object has no attribute 'get'

CodePudding user response:

Jason Harper in the comments has the right answer. Separating the .pack() statements from the Label and Entry definitions will fix the error.

Here is what I got

import tkinter as tk

root = tk.Tk()

root.title("Matchstick Game")
#root.iconbitmap("match.ico")
root.geometry("1000x580")

H = tk.Entry(master = root, width = 80, fg = 'red')
H.pack()

def clickme():
    mylabel = tk.Label(root, text = ( "Hello"   H.get()))
    mylabel.pack()

my_button = tk.Button(root, text = "whats your name", 
padx=10,pady=10,bg='white',fg='green',command=clickme)

my_button.pack()

root.mainloop()

Hope that helped.

CodePudding user response:

pack() function of Entry returns None, so you're storing None in H. What you want to do is store the Entry object in H, and then call pack method on it later.

H = Entry(root, width = 80, fg = 'red')
H.pack()
  • Related