python – 双击时Tkinter程序不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 双击时Tkinter程序不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3394字,纯文字阅读大概需要5分钟。
内容图文
![python – 双击时Tkinter程序不起作用](/upload/InfoBanner/zyjiaocheng/729/150bea5d8a3a431f8757d93b6a33d414.jpg)
我对tkinter很新,做了一个简单的小程序:
! 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)
如果我通过在IDLE中按F5执行它,这段代码工作得很好,但是当我双击它的快捷方式时,它会抛出这个错误:
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
我已经尝试从终端,其他在线帖子执行它,但它只是一直抛出这个错误/另一个“无法导入”错误/ NameErrors.
此外,当我运行该程序时,创建了一个文件夹(pycache),其中有一个名为tkinter.cpython-37.pyc的文件
我在Windows 10上使用python 3.7,Python添加到PATH,我的其他python安装被禁用.
解决方法:
您的桌面上有一个名为tkinter.py的文件.在其模块搜索路径中包含桌面的任何程序都将无法找到内置tkinter模块,因为它们具有相同的名称.
尝试将该文件重命名为其他内容.
内容总结
以上是互联网集市为您收集整理的python – 双击时Tkinter程序不起作用全部内容,希望文章能够帮你解决python – 双击时Tkinter程序不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。