所以我确实对此进行了研究,并找到了几种方法来做到这一点,但由于某些原因我每次尝试这样做,它只是不适合我,我希望有人能够帮助我。对于我的程序,我目前首先弹出一个消息框,这是我刚给出的代码,但我要做的是从消息框中取出文本名称并将其传递给类外的函数,它可以打开文件。感谢
class MyDialog:
def __init__(self, parent):
top = self.top = Toplevel(parent)
top.wm_attributes("-topmost", 1)
Label(top, text="Enter File Name").pack()
self.textboxpopup = Entry(top)
self.textboxpopup.pack(padx=5)
popupButton = Button(top, text="Submit", command=self.popUpinfo)
popupButton.pack(pady=5)
def popUpinfo(self):
startfilename = self.textboxpopup.get()
self.top.destroy()
return startfilename
def read_file():
startfilename = MyDialog(root).popUpinfo()
print(startfilename)
openfile = open(startfilename + ".txt")
lines = openfile.read().split('\n')
lines = [l.split(',') for l in lines]
return lines