假设我有一个包含10个数字的文本文件
我已经编写了一个代码来 Select 并打开文件,默认窗口显示一个按钮来 Select 它和一个空的组合框
我想在打开文件但不关闭窗口时,用文件中的值填充组合框
from tkinter.filedialog import askopenfilename
import os
import tkinter as tk
from tkinter import ttk
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
ButtonSelect_File = tk.Button(self, text='Select file', command=self.displayPath)
ButtonSelect_File.grid(row=1, column=0)
self._File = tk.StringVar(self)
self._File.set('')
label_File = tk.Label(self, textvariable=self._File)
label_File.grid(row=1, column=1)
self.combobox = ttk.Combobox(self, values=[])
self.combobox.grid(row=2, column=0)
def displayPath(self):
self.File = askopenfilename()
self.directory = os.path.split(self.File)[0]
self._File.set(self.File)
def readFile(self, file):
read = open(file, "r")
values = [i for i in read]
### HOW TO ADD THESE VALUES TO THE COMBOBOX ####
if __name__ == "__main__":
obj = Application()
obj.mainloop()
obj.readFile(obj.File)
有人能告诉我怎么做吗?