![](/img/trans.png)
[英]Starting an external process after opening a new tkinter window. [Python]
[英]why is the location string not showing in the window. tkinter python?
我正在尝试编写一个代码来告诉您该位置是文件还是目录。 如果它是一个文件,那么它将读取该文件。 这是我的代码(它非常糟糕,我很抱歉)
import os
import tkinter as tk
screen = tk.Tk()
screen.title("files and directories")
screen.geometry("300x100")
def FileDir():
path = location.get(1.0, "end-1c")
location.delete(1.0, tk.END)
if os.path.exists(path):
print("✔ - this location exists")
info_location = tk.Label(screen, text=f"location: {location}")
info_location.pack()
if os.path.isfile(path):
print("\tthis is a file")
type = 'file'
info_type = tk.Label(screen, text=f"type: {type}")
info_type.pack()
while True:
open_file = input("\nDo you want to read this file? ")
if open_file.lower() == 'yes':
with open(path) as file:
contents = file.read()
print(contents)
break
elif open_file.lower() == 'no':
print("goodbye!")
break
else:
print("invalid input")
continue
elif os.path.isdir(path):
print("\tthis is a directory")
type = 'directory'
info_type = tk.Label(screen, text=f"type: {type}")
info_type.pack()
else:
print("✘ - this location doesn't exist")
text = tk.Label(screen, text="Enter file/directory location: ")
text.pack()
location = tk.Text(screen, height = 1, width = 25)
location.pack()
enter_btn = tk.Button(screen, text="Enter", command=FileDir)
enter_btn.pack()
screen.mainloop()
因此,在放置字符串的位置时,一切正常,只是位置没有显示,而是显示“.!text”。 有谁知道为什么?
location<\/code>是一个小部件而不是一个字符串。
tkinter 小部件的字符串表示是它的内部名称。
因此,当您执行
text=f"location: {location}"<\/code>时,您正在创建一个包含小部件名称而不是内容的字符串。
要显示内容,您必须从小部件中获取它们。 当你定义
path<\/code>时你已经这样做了,所以你只需要使用
{path}<\/code>而不是
{location}<\/code>
text=f"location: {path}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.