Python:OpenFile 由于无法访问文本变量,对话框没有出现

Python:OpenFile dialog did not appear because of cannot access the text variable

我已经为文件对话框创建了一个按钮、一个条目和一个函数。在我按下 'browse' 按钮后,文件对话框没有出现,所以我怎样才能确保我的 custName.set(filename) 可以在我的 TracingMethod () 函数中使用。

我创建文件对话框的步骤:

第 1 步:从 tkinter 导入文件对话框

from tkinter import filedialog

第 2 步:创建按钮和条目

self.browseButton = Button(self.radioframe, text="Browse",command = self.open)
self.browseButton.grid(row =3, column =3, sticky = W)

#Define the entry
custName = StringVar(None)
location_ent =Entry(self.radioframe,textvariable = custName)
location_ent.grid(row =2, column = 2, sticky = W,columnspan=1)
location_ent.update()
location_ent.focus_set()

第 3 步:创建 open() 函数[IDE 此处显示错误]

def open():
    filename = filedialog.askopenfilename(parent=root,title='Choose a file')
    custName.set(filename)

我在想我是在独家新闻之外定义函数吗? 所以我提供了完整的代码以供参考。

from tkinter import *
from tkinter import filedialog

class TracingInterface(Frame):
    def __init__(self, master):
        super().__init__()
        master.minsize(width=700, height=520)
        master.maxsize(width=700, height=520)
        Grid.config(self)
        self.TracingMethod()
        self.logDetails()
        self.otherFunctionInterface()


    def TracingMethod(self):

        self.traceMethodSelect = StringVar()
        self.traceMethodSelect.set("LT")

        self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =300)
        self.radioframe.grid(row= 0, column=0)
        self.radioframe.grid_propagate(0)

        self.radioframe.LT= Radiobutton(
        self.radioframe, text="Live Tracing",
        variable=self.traceMethodSelect, value="LT",
        anchor=W).grid(row=1, column = 0, sticky = W,columnspan=2)
        self.radioframe.SL= Radiobutton(
        self.radioframe, text="Specific Location",
        variable=self.traceMethodSelect, value="SL",
        anchor=W).grid(row=2, column = 0, sticky = W,columnspan=2)

        self.traceButton = Button(self.radioframe, text="Trace")
        self.traceButton.grid(row =3, column =0, sticky = W)

        self.cancelButton = Button(self.radioframe, text="Cancel")
        self.cancelButton.grid(row =3, column =1, sticky = W)

        self.configUAButton = Button(self.radioframe, text="Configuration",command=printMessage)
        self.configUAButton.grid(row =3, column =2, sticky = W)
        self.configUAButton.config(width=15)

        self.browseButton = Button(self.radioframe, text="Browse",command = self.open)
        self.browseButton.grid(row =3, column =3, sticky = W)

        custName = StringVar(None)
        location_ent =Entry(self.radioframe,textvariable = custName)
        location_ent.grid(row =2, column = 2, sticky = W,columnspan=1)
        location_ent.update()
        location_ent.focus_set()

    def logDetails(self):
        self.logframe = LabelFrame(self,text="Log Details",height= 520,width =390,padx=15)
        self.logframe.grid_propagate(0)

        self.logframe.grid_rowconfigure(0,weight =1)
        self.logframe.grid_columnconfigure(0,weight=1)

        xscrollbar = Scrollbar(self.logframe,orient = HORIZONTAL)
        xscrollbar.grid(row=1, column=1, sticky=E+W,columnspan=2)

        yscrollbar = Scrollbar(self.logframe,orient= VERTICAL)
        yscrollbar.grid(row=0, column=3, sticky=N+S)

        text = Text(self.logframe,width=50,height=50, wrap=NONE, xscrollcommand=xscrollbar.set,
                yscrollcommand=yscrollbar.set)
        text.grid(row=0, column=1, columnspan=2)
        # attach listbox to scrollbar
        xscrollbar.config(command=text.xview)
        yscrollbar.config(command=text.yview)

        button_1 = Button(self.logframe, text="View", command=printMessage,width =10)
        button_1.grid(row=2,column= 1)

        button_2 = Button(self.logframe, text="Export", command=printMessage,width =10)
        button_2.grid(row=2,column= 2)

        self.logframe.grid(row=0,column =1,rowspan=5)

    def otherFunctionInterface(self):
        self.otherFrame = LabelFrame(self,text="Other Function",height= 400,width =300)
        self.otherFrame.grid(row=4, column=0)
        self.otherFrame.grid_propagate(0)

        OpenPreviousCaseFile = Button(self.otherFrame, text="Open previous case file", command=printMessage,height = 4,
                                  width =21)
        OpenPreviousCaseFile.grid(row=5,column= 0,pady=5,padx=5)

        OpenPreviousTracingResult = Button(self.otherFrame, text="Open last tracing result ", command=printMessage,
                                       height = 4, width =21)
        OpenPreviousTracingResult.grid(row=6,column= 0,pady=5,padx=5)

        OpenMenualbtn = Button(self.otherFrame, text="User manual", command=printMessage,height =4, width =21)
        OpenMenualbtn.grid(row=7,column= 0,pady=5,padx=5)

        AboutBtn = Button(self.otherFrame, text="About", command=printMessage,height = 4, width =21)
        AboutBtn.grid(row=8,column= 0,pady=5,padx=5)

        locateCaseFile = Entry(self.otherFrame)
        locateCaseFile.grid(row=5,column = 1)

        locateTraceResult = Entry(self.otherFrame)
        locateTraceResult.grid(row=6,column = 1)
def open():
        filename = filedialog.askopenfilename(parent=root,title='Choose a file')
        custName.set(filename)


def     printMessage():
        print("Wow this actually worked!")

if __name__=='__main__':
    root=Tk()
    root.title("Windows User Activity History Tracing and Analysing System")
    tif= TracingInterface(root)
    root.mainloop()

你这里有几个错误:

  • custName(当然还有 location_ent)应该是 class
  • 的属性
  • open 函数必须是您的 TracingInterface class 的方法并且 return 字符串必须分配给 self.custName :

    class TracingInterface(Frame):
        # ...
        def TracingMethod(self):
            # ...
            self.custName = StringVar(None)
            self.location_ent =Entry(self.radioframe,textvariable = self.custName)
            self.location_ent.grid(row =2, column = 2, sticky = W,columnspan=2)
            self.location_ent.update()
            self.location_ent.focus_set()
    
        def open(self):
            filename = filedialog.askopenfilename(parent=root,title='Choose a file')
            self.custName.set(filename)
    

顺便说一句,为了看到这个按钮,我不得不将 location_ent 的列跨度从 1 更改为 2,并将主要部分及其左侧部分的宽度增加到 100:

# Increase global width
master.minsize(width=800, height=520)
master.maxsize(width=800, height=520)
# ...
# Increase TracingMethod width
self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =400)
# ...
# Increase columnspan
location_ent.grid(row =2, column = 2, sticky = W,columnspan=2)
# ...
# Increase otherFunction width
self.otherFrame = LabelFrame(self,text="Other Function",height= 400,width =400)

self.browseButton 小部件没有出现,因为您将 location_entcolumnspan 选项设置为 1。您必须将其修改为 2,因为那里也有 self. configUAButton 按钮.

这意味着您需要更改:

location_ent.grid(row =2, column = 2, sticky = W,columnspan=1)

收件人:

location_ent.grid(row =2, column = 2, sticky = W,columnspan=2)

但是你现在几乎不能 self.browseButto 因为你为 self.radioframe 设置的宽度不够。所以改变这一行:

self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =300)

至:

self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =355)

当然,您需要相应地修改masterwindow的width选项。为此,更改:

master.minsize(width=700, height=520)
master.maxsize(width=700, height=520)

最后,您无法到达 custName,因为您需要将其设为 instance variable。这意味着您需要更改:

self.custName = StringVar(None)
location_ent =Entry(self.radioframe,textvariable = self.custName)

收件人:

self.custName = StringVar(None)
location_ent =Entry(self.radioframe,textvariable = self.custName)

收件人:

master.minsize(width=755, height=520)
master.maxsize(width=755, height=520)

并且(因此)在 open() 方法中:

custName.set(filename)

收件人:

self.custName.set(filename)

完整节目

让我们把前面提到的所有内容放在一起:

from tkinter import *
from tkinter import filedialog

class TracingInterface(Frame):
    def __init__(self, master):
        super().__init__()
        master.minsize(width=755, height=520)
        master.maxsize(width=755, height=520)
        Grid.config(self)
        self.TracingMethod()
        self.logDetails()
        self.otherFunctionInterface()


    def TracingMethod(self):

        self.traceMethodSelect = StringVar()
        self.traceMethodSelect.set("LT")
        # Bill
        self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =355)
        self.radioframe.grid(row= 0, column=0)
        self.radioframe.grid_propagate(0)

        self.radioframe.LT= Radiobutton(
        self.radioframe, text="Live Tracing",
        variable=self.traceMethodSelect, value="LT",
        anchor=W).grid(row=1, column = 0, sticky = W,columnspan=2)
        self.radioframe.SL= Radiobutton(
        self.radioframe, text="Specific Location",
        variable=self.traceMethodSelect, value="SL",
        anchor=W).grid(row=2, column = 0, sticky = W,columnspan=2)

        self.traceButton = Button(self.radioframe, text="Trace")
        self.traceButton.grid(row =3, column =0, sticky = W)

        self.cancelButton = Button(self.radioframe, text="Cancel")
        self.cancelButton.grid(row =3, column =1, sticky = W)

        self.configUAButton = Button(self.radioframe, text="Configuration",command=printMessage)
        self.configUAButton.grid(row =3, column =2, sticky = W)
        self.configUAButton.config(width=15)

        self.browseButton = Button(self.radioframe, text="Browse",command = self.open)
        self.browseButton.grid(row =3, column =3, sticky = W)
        # Bill
        self.custName = StringVar(None)
        location_ent =Entry(self.radioframe,textvariable = self.custName)
        # Bill
        location_ent.grid(row =2, column = 2, sticky = W,columnspan=2)
        location_ent.update()
        location_ent.focus_set()

    def logDetails(self):
        self.logframe = LabelFrame(self,text="Log Details",height= 520,width =390,padx=15)
        self.logframe.grid_propagate(0)

        self.logframe.grid_rowconfigure(0,weight =1)
        self.logframe.grid_columnconfigure(0,weight=1)

        xscrollbar = Scrollbar(self.logframe,orient = HORIZONTAL)
        xscrollbar.grid(row=1, column=1, sticky=E+W,columnspan=2)

        yscrollbar = Scrollbar(self.logframe,orient= VERTICAL)
        yscrollbar.grid(row=0, column=3, sticky=N+S)

        text = Text(self.logframe,width=50,height=50, wrap=NONE, xscrollcommand=xscrollbar.set,
                yscrollcommand=yscrollbar.set)
        text.grid(row=0, column=1, columnspan=2)
        # attach listbox to scrollbar
        xscrollbar.config(command=text.xview)
        yscrollbar.config(command=text.yview)

        button_1 = Button(self.logframe, text="View", command=printMessage,width =10)
        button_1.grid(row=2,column= 1)

        button_2 = Button(self.logframe, text="Export", command=printMessage,width =10)
        button_2.grid(row=2,column= 2)

        self.logframe.grid(row=0,column =1,rowspan=5)

    def otherFunctionInterface(self):
        self.otherFrame = LabelFrame(self,text="Other Function",height= 400,width =300)
        self.otherFrame.grid(row=4, column=0)
        self.otherFrame.grid_propagate(0)

        OpenPreviousCaseFile = Button(self.otherFrame, text="Open previous case file", command=printMessage,height = 4,
                                  width =21)
        OpenPreviousCaseFile.grid(row=5,column= 0,pady=5,padx=5)

        OpenPreviousTracingResult = Button(self.otherFrame, text="Open last tracing result ", command=printMessage,
                                       height = 4, width =21)
        OpenPreviousTracingResult.grid(row=6,column= 0,pady=5,padx=5)

        OpenMenualbtn = Button(self.otherFrame, text="User manual", command=printMessage,height =4, width =21)
        OpenMenualbtn.grid(row=7,column= 0,pady=5,padx=5)

        AboutBtn = Button(self.otherFrame, text="About", command=printMessage,height = 4, width =21)
        AboutBtn.grid(row=8,column= 0,pady=5,padx=5)

        locateCaseFile = Entry(self.otherFrame)
        locateCaseFile.grid(row=5,column = 1)

        locateTraceResult = Entry(self.otherFrame)
        locateTraceResult.grid(row=6,column = 1)
    def open(self):
        filename = filedialog.askopenfilename(parent=root,title='Choose a file')
        # Bill
        self.custName.set(filename)


def     printMessage():
        print("Wow this actually worked!")

if __name__=='__main__':
    root=Tk()
    root.title("Windows User Activity History Tracing and Analysing System")
    tif= TracingInterface(root)
    root.mainloop()

演示

下面是运行上面程序的截图: