1

I already created a button, an entry and a function for file dialog. After i press the 'browse' button the file dialog did not appear, so how can i make sure my custName.set(filename) can be use in my TracingMethod () function.

Steps for me to create a file dialog:

Step 1: import the file dialog from tkinter

from tkinter import filedialog

Step 2: Create the button and the entry

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

Step 3: Create the open() function [The IDE shows an error here]

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

What I am thinking is am I define the function outside the scoop? So I provide the full coding for reference.

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 Answers 2

3

self.browseButton widget does not appear because you set the columnspan option for location_ent to 1. You must modify it to 2 because you have self. configUAButton button there too.

This means you need to change:

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

To:

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

But you barely can self.browseButto now because the width you set for self.radioframe is not sufficient. So change this line:

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

to:

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

Of course, you will need to consequently modify the width option of master window. For this, change:

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

Finally, you can not reach custName because you need to make it as an instance variable instead. This means you need to change:

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

To:

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

To:

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

And (consequently) in open() method:

custName.set(filename)

To:

self.custName.set(filename)

Full program

Let us put all what is previously mentioned together:

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

Demo

Here is a screenshot of the running above program:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You have several mistakes here:

  • custName (and certainly location_ent) should be an attribute of the class
  • The open function must be a method of your TracingInterface class and the return string must be assigned to 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)
    

Btw to see the button, I had to change the columnspan of location_ent from 1 to 2 and increase the width of the main and its left part of 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.