python - 好吧,这很奇怪 Python
问题描述
好吧,当我收到此错误时,我只是在使用 TKInter NameError name 'runApps' is not defined
import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
apps = []
def addApp():
for widget in frame.winfo_children():
widget.destroy()
filename = filedialog.askopenfilename(initialdir="/", title="Select File",
filetypes=(("executables","*.exe"), ("all files", "*.*")))
apps.append(filename)
print(filename)
for app in apps:
label = tk.Label(frame, text=app, bg="gray")
label.pack()
def runApps():
for app in apps:
os.startfile(app)
canvas = tk.Canvas(root, height=700, width=700, bg="#163542")
canvas.pack()
frame = tk.Frame(root, bg="green")
frame.place(relwidth=0.4, relheight=0.4, relx=0.3, rely=0.25)
openFile = tk.Button(root, text="Open File", padx=10,
pady=5, fg="white", bg="#163542" ,command=addApp)
openFile.pack()
runApps = tk.Button(root, text="Run Apps", padx=10,
pady=5 , fg="white", bg="#163542" , command = runApps)
runApps.pack()
root.mainloop()
好的,如果有人知道如何解决此问题,请发表评论或回答此帖子的此部分仅适用于 ShanyeLoyd 这是您想要的帖子 ShanyeLoyd
解决方案
如果您修复代码中的缩进,那么它可以工作。
根据官方文档:
逻辑行开头的前导空格(空格和制表符)用于计算行的缩进级别,而缩进级别又用于确定语句的分组。
正确格式化代码:
import tkinter as tk
from tkinter import filedialog
import os
root = tk.Tk()
apps = []
def addApp():
for widget in frame.winfo_children():
widget.destroy()
filename = filedialog.askopenfilename(
initialdir="/",
title="Select File",
filetypes=(("executables", "*.exe"), ("all files", "*.*")),
)
apps.append(filename)
print(filename)
for app in apps:
label = tk.Label(frame, text=app, bg="gray")
label.pack()
def runApps():
for app in apps:
os.startfile(app)
canvas = tk.Canvas(root, height=700, width=700, bg="#163542")
canvas.pack()
frame = tk.Frame(root, bg="green")
frame.place(relwidth=0.4, relheight=0.4, relx=0.3, rely=0.25)
openFile = tk.Button(
root, text="Open File", padx=10, pady=5, fg="white", bg="#163542", command=addApp
)
openFile.pack()
runApps = tk.Button(
root, text="Run Apps", padx=10, pady=5, fg="white", bg="#163542", command=runApps
)
runApps.pack()
root.mainloop()
图形用户界面:
推荐阅读
- elasticsearch - 如何查看来自 Elasticsearch 的线级响应
- android - 如何排列LinearLayout内的项目以匹配android上的父宽度
- reactjs - 如何在弹出的 CRA 和 Storybook 中使用 PostCSS 插件
- pandas - Pandas DataFrame“信息”方法
- c++ - 我在使用 istringstream 类时遇到了一些麻烦
- tableau-api - 为什么更改数据源后我的表格图消失了?
- postgresql - 如果我在查询中多次使用它,Postgres 会缓存 SIMILARITY(或任何昂贵的函数调用)结果吗?
- python - python速成课程学习日志项目没有反向匹配错误
- node.js - 向服务器请求后无法返回组件 - MERN
- java - 我想通过我的应用程序发送 wattsapp 消息,而不打开 wattsapp