user7623610
user7623610

Reputation: 97

Simple GUI in Python using tkinter

I am trying to create a simple GUI in python using tkinter. What I am trying to do is

  1. Place my entry element in the center of the upper half of the GUI window
  2. Place a button right next to it
  3. On clicking button, open up an interface to choose a file
  4. 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.

Upvotes: 1

Views: 1390

Answers (1)

Mia
Mia

Reputation: 2676

Here is the revised code. Basically, you need to pass a function object to the command argument of a Button, which means you can either pass a function without the trailing parenthesis (if it doesn't take any argument) or use lambda. In your original code, your function was executed immediately after the python interpreter reaches that line. Also, you need to call root.mainloop at the end of the program.

import tkinter
import tkinter.filedialog
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()
frame=tkinter.Frame(root)
root.entry = tkinter.Entry(frame,textvariable=root.entryVariable)
B = tkinter.Button(frame, text ="Choose", command=lambda: OnButtonClick(root))
root.entry.grid(column=0,row=0)
B.grid(column=1,row=0)
frame.pack(pady=100)  #Change this number to move the frame up or down
root.mainloop()

Upvotes: 3

Related Questions