无法将默认文本插入 TKinter

can't insert default text into TKinter

所以我正在使用 TKinter 创建一个文本编辑器。我已经掌握了基础知识,现在我正在尝试创建一个按钮,当您按下它时,您 select 一个文件并打开它。据我所知,insert 方法最适合 Entry 小部件。但我正在使用标签小部件,但由于某种原因它不起作用。到目前为止,这是我的代码:

import sys
import tkinter as tk 
frame = tk.Tk() 
frame.title("Fake Google Docs") 
frame.geometry('800x600') 

inputtxt = tk.Text(frame, 
                height = 25, 
                width = 90) 

inputtxt.pack() 

global file
 
#this is function to be executed when u press first button
def printInput(): 
  filename = input("Name of the file make sure you end with .txt")
  file = open(filename, 'w')
  result=inputtxt.get("1.0","end")
  file.write(result) 
  file.close()
  
#this is function to be executed for open new file thing
def printInputnew(): 
  filenamemm = input("Name of the file make sure you end with .txt")
  file = open(filenamemm, 'w')
  result=inputtxt.get("1.0","end")
  file.write("") 
  file.close()

def openfile(): 
  filenamem = input("Name of the file you want to open")
  tru=open(filenamem)
  if tru== False:
    sys.exit() 
  else:
    f = open(filenamem, "r")
    for x in f:
      print(x)
      name=str(x)
    text = tk.StringVar() 
    text.set(name) 
    
 #button 1
printButton = tk.Button(frame, 
                        text = "Save to text file", 
                        command = printInput) 
printButton.pack() 

printButtonl = tk.Button(frame, 
                        text = "open a file", 
                        command = openfile) 
printButtonl.pack() 
#button 2
printButton = tk.Button(frame, 
                        text = "Open new file", 
                        command = printInputnew) 
printButton.pack() 
#open the text box
lbl = tk.Label(frame, textvariable = "") 
lbl.pack() 

 #finish code
frame.mainloop() 

其他人能否建议一种将文本插入此小部件的方法?

这里是:

import sys
import tkinter as tk
from tkinter import filedialog

frame = tk.Tk()
frame.title("Fake Google Docs")
frame.geometry('800x600')

inputtxt = tk.Text(frame,
                   height=25,
                   width=90)

inputtxt.pack()

global file

mask = [("Text and Python files", "*.txt *.py *.pyw *.rcad"),
             ("HTML files", "*.htm"),
             ("All files", "*.*")]


# this is function to be executed when u press first button
def printInput():
    filename = input("Name of the file make sure you end with .txt")
    file = open(filename, 'w')
    result = inputtxt.get("1.0", "end")
    file.write(result)
    file.close()


# this is function to be executed for open new file thing
def printInputnew():
    filenamemm = input("Name of the file make sure you end with .txt")
    file = open(filenamemm, 'w')
    result = inputtxt.get("1.0", "end")
    file.write("")
    file.close()


def openfile():
    filenamem = filedialog.askopenfilename(filetypes=mask)
    tru = open(filenamem)
    if tru == False:
        sys.exit()
    else:
        f = open(filenamem, "r")
        inputtxt.delete(0.0, 'end')
        inputtxt.insert('end', f.read())

        # button 1


printButton = tk.Button(frame,
                        text="Save to text file",
                        command=printInput)
printButton.pack()

printButtonl = tk.Button(frame,
                         text="open a file",
                         command=openfile)
printButtonl.pack()
# button 2
printButton = tk.Button(frame,
                        text="Open new file",
                        command=printInputnew)
printButton.pack()
# open the text box
lbl = tk.Label(frame, textvariable="")
lbl.pack()

# finish code
frame.mainloop() 

我更改了这段代码:

def openfile():
    filenamem = filedialog.askopenfilename(filetypes=mask)
    tru = open(filenamem)
    if tru == False:
        sys.exit()
    else:
        f = open(filenamem, "r")
        inputtxt.delete(0.0, 'end')
        inputtxt.insert('end', f.read())

它会打开一个 window,询问您要打开哪个文件。 askopenfilename 方法 returns 所选文件的字符串路径。然后您只需读取整个文件,删除 tk.Text 对象中的整个文本,然后将打开的文件中的文本粘贴到那里。就是这样:)
下次请在询问和阅读 PEP8 之前先看一些关于 tkinter 的指南。