【问题标题】:Python:OpenFile dialog did not appear because of cannot access the text variablePython:打开文件对话框没有出现,因为无法访问文本变量
【发布时间】:2016-11-20 16:18:39
【问题描述】:

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

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

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

from tkinter import filedialog

第二步:创建按钮和入口

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()

【问题讨论】:

    标签: python tkinter openfiledialog


    【解决方案1】:

    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)
    

    当然,您需要相应地修改master 窗口的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()
    

    演示

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

    【讨论】:

      【解决方案2】:

      这里有几个错误:

      • 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,并将 main 及其左侧部分的宽度增加 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)
      

      【讨论】:

        猜你喜欢
        • 2018-05-03
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        • 2015-12-03
        • 2021-08-31
        • 2016-10-16
        • 2016-02-18
        • 2017-09-29
        相关资源
        最近更新 更多