I am trying to create a simple GUI in python using tkinter. What I am trying to do is
- Place my entry element in the center of the upper half of the GUI window
- Place a button right next to it
- On clicking button, open up an interface to choose a file
Display the file name along with its path in the entry element
def center_window(width, height): # get screen width and height screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() # calculate position x and y coordinates x = (screen_width/2) - (width/2) y = (screen_height/2) - (height/2) root.geometry('%dx%d+%d+%d' % (width, height, x, y)) def OnButtonClick(self): self.entryVariable.set( tkinter.filedialog.askopenfilename() ) self.entry.focus_set() self.entry.selection_range(0, tkinter.END) root = tkinter.Tk() center_window(400, 300) root.title("A simple GUI") root.entryVariable = tkinter.StringVar() root.entry = tkinter.Entry(root,textvariable=root.entryVariable) root.entry.grid(column=10,row=5,columnspan=20) B = tkinter.Button(root, text ="Choose", command=OnButtonClick(root)) B.grid(column=30,row=5, columnspan=2)
Could anybody guide me how can I move entry element and button in the center of the upper half of the GUI window. Also, how can I make tkinter.filedialog.askopenfilename() function to be invoked on clicking the button. It gets invoked as soon as the GUI window opens when I run the above code. Thanks.