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

时间:2016-07-18 06:53:38

标签: python tkinter openfiledialog

我已经创建了一个按钮,一个条目和一个文件对话框功能。在我按下“浏览”后按钮文件对话框没有出现,所以如何确保我的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()

2 个答案:

答案 0 :(得分:3)

self.browseButton窗口小部件未显示,因为您将columnspan的{​​{1}}选项设置为1.您必须将其修改为2,因为您也有location_ent按钮。< / p>

这意味着您需要更改:

self. configUAButton

要:

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

但你现在几乎不能location_ent.grid(row =2, column = 2, sticky = W,columnspan=2) o因为你为self.browseButt设置的宽度不够。所以改变这一行:

self.radioframe

为:

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

当然,您需要修改self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =355) 窗口的width选项。为此,请更改:

master

最后,您无法访问master.minsize(width=700, height=520) master.maxsize(width=700, height=520) ,因为您需要将其设为instance variable。这意味着您需要更改:

custName

要:

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)

演示

以下是运行上述程序的屏幕截图:

enter image description here

答案 1 :(得分:1)

你有几个错误:

  • custName(当然location_ent)应该是班级的属性
  • open函数必须是TracingInterface类的方法,并且必须将返回字符串分配给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)