FlamingBlizzard
FlamingBlizzard

Reputation: 21

Tk is not defined

this opens a new window when the button Open is clicked but when the new window open nothing is shown about password thing i put but i get tk is not defined
import tkinter as tk

        def Open():
            newWindow = tk.Toplevel(MyProgram)
            // right here Tk isnt defined but idk what to do ?
            top = Tk()
            // how tall the window should be  
            top.geometry("450x300")
            // label for username
            user_name = Label(top, 
                          text = "Username").place(x = 40,
            // fill in ur password                 y = 60)  
            user_password = Label(top, 
                              text = "Password").place(x = 40,
            //submit ur answer                         y = 100)  
            submit_button = Button(top, 
                               text = "Submit").place(x = 40,
            // for the username                       y = 130)
            user_name_input_area = Entry(top,
                                     width = 30).place(x = 110,
            // for the username                        y = 60)  
            user_password_entry_area = Entry(top,
                                         width = 30).place(x = 110,
                                                           y = 100)  
            // this is to finish it
            top.mainloop() 
        
        
        
       // this opens a window saying quit and Open 
        // name of tk
        MyProgram = tk.Tk()
        // frame for the window
        frame = tk.Frame(MyProgram)
        // to make frame work you add this
        frame.pack()
        // a button with quit on it to end the program
        button = tk.Button(frame, 
                           text="QUIT", 
                           fg="red",
                           command=quit)

        button.pack(side=tk.LEFT)

        slogan = tk.Button(frame,
                           text="Open",
                           command=Open)

        slogan.pack(side=tk.LEFT)
        
        MyProgram.mainloop()

when i run this it opens the first window which is QUIT and Open,Quit works but ,Open does open a new window but it doesn't show password and username because Tk is not defined I don't know what I'm doing wrong can you help please?

Upvotes: 0

Views: 124

Answers (3)

Alok Dubey
Alok Dubey

Reputation: 64

If you got this problem try this:-

import tkinter
from tkinter import *
root = tkinter.Tk
root.mainloop()

Upvotes: 0

user15801675
user15801675

Reputation:

In your code, you have done:

top=Tk()

You know that Tk() is a class in the __init__ file in tkinter folder(Windows 10, I don't know about mac or linux).

At the top of the code, you have imported tkinter folder as tk. So whenever you want to use a class from it, you will use tk.<widget>.

import tkinter as tk

However, in your code, you just mentioned Tk(). Now, python doesn't know what are you trying to do. In your code, Tk() is neither a function, nor a class. Also, it doesn't know you are referencing it from tkinter folder.

Now that would have been True if you had done a wild-card import or you had imported everything.

from tkinter import *

The fix is:

top=tk.Tk()

Upvotes: 0

Punker
Punker

Reputation: 1878

Tk is a module of tkinter package, so you have to call it specifying the name of underlying package. Since you've imported tkinter as tk, try replacing:

top = Tk()

with

top = tk.Tk()

Upvotes: 1

Related Questions