简体   繁体   中英

Tkinter program doesn't work when double-clicked

I am pretty new to tkinter and made a simple little program:

! python3

from tkinter import *
import tkinter.messagebox
import time

checked = False
pressed = False
rthat = False
rthat2 = False
fe = True

root = Tk()

def hw():
    print('Hello World!')

def custom():
    wp = input('Input: ')
    print(wp)

def Except():
    wp = input('Input: ')
    raise Exception(wp)

def ChCh():
    global checked
    if checked:
        checked = False
    else:
        checked = True

def cPop():
    global fe
    user = input('Input (WINDOW TITLE): ')
    user2 = input('Input (TEXT): ')
    user3 = input('What type do you want? ("ERROR"/"WARNING"/"INFO"): ')
    if user3 == 'INFO':
        tkinter.messagebox.showinfo(user, user2)
    elif user3 == 'ERROR':
        tkinter.messagebox.showerror(user, user2)
    elif user3 == 'WARNING':
        tkinter.messagebox.showwarning(user, user2)
    else:
        tkinter.messagebox.showinfo(user, user2)
    if fe:
        fe = False
        tkinter.messagebox.askquestion('First Experience', 'Was your experience with this program good?')

def destrth():
    explainL.destroy()
    check.destroy()
    Pass.destroy()
    BackB.destroy()
    Submit.destroy()
    startS()

def submitted():
    global Invalid, Unchecked, rthat, rthat2
    if checked:
        if Pass.get() == "banana":
            root.destroy()
        else:
            if rthat2:
                rthat2 = False
                Unchecked.destroy()
            if rthat:
                Invalid.destroy()
            Invalid = Label(root, text="Invalid password!", fg="red")
            rthat = True
            Invalid.pack()
    else:
        if rthat:
            rthat = False
            Invalid.destroy()
        if rthat2:
            Unchecked.destroy()
        Unchecked = Label(root, text="Please check the CheckBox!", fg="red")
        rthat2 = True
        Unchecked.pack()

def exit1():
    root.geometry("400x150")
    global explainL, check, Pass, BackB, Submit
    subMenu.destroy()
    ExitMenu.destroy()
    button.destroy()

    explainL = Label(root, text="Enter password: ")
    check = Checkbutton(root, text="I Agree to the fact that pressing SUBMIT will close this window.", command=ChCh)
    Pass = Entry(root)
    BackB = Button(root, text="BACK", command=destrth)
    Submit = Button(root, text="SUBMIT", command=submitted)
    explainL.pack()
    Pass.pack()
    check.pack()
    Submit.pack()
    BackB.pack(side=RIGHT)

def startS():
    root.geometry("200x220")
    global subMenu, button, ExitMenu
    if rthat2:
        Unchecked.destroy()
    if rthat:
        Invalid.destroy()

    button = Button(root, text="Quit", command=exit1, height=5, width=5)

    menu = Menu(root)
    root.config(menu=menu)

    subMenu = Menu(menu)
    menu.add_cascade(label="print", menu=subMenu)
    subMenu.add_command(label="Hello World!", command=hw)
    subMenu.add_command(label="Custom", command=custom)
    subMenu.add_separator()
    subMenu.add_command(label="Raise Exception (CUSTOM)", command=Except)
    subMenu.add_command(label="Show PopUp (CUSTOM)", command=cPop)

    ExitMenu = Menu(menu)
    menu.add_cascade(label="Exit", menu=ExitMenu)
    ExitMenu.add_command(label="Quit", command=exit1)

    button.pack(side=LEFT)
    button.place(relx=0.5, rely=0.5, anchor=CENTER)


startS()

root.mainloop()
time.sleep(7)

This code works just fine if I execute it by pressing F5 in the IDLE, but when I double-click the shortcut to it, it throws out this error:

Traceback (most recent call last):
  File "moreTk.py", line 3, in <module>
    import tkinter
  File "C:\Users\RolandPC\Desktop\tkinter.py", line 8, in <module>
    root = Tk()
NameError: name 'Tk' is not defined

I have tried executing it from the terminal, other Online posts, but it just kept throwing out this error / another "could not import that" error / NameErrors. Also, when I ran the program there was a folder created ( pycache ) in which there is a file called tkinter.cpython-37.pyc I'm using python 3.7 on Windows 10, Python is added to PATH, and my other installations of python were disabled.

You have a file on your desktop named tkinter.py . Any program that includes the desktop in its module search path will be unable to find the built-in tkinter module because they have the same name.

Try renaming that file to something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM