Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphical user interface with TK - button position and actions

Tags:

python

tk

I started using TK in python to build a graphical interface for my program. I'm not able to fix 2 issues concerning (1) the position of a button in the window and (2) use a value of a radiobutton inside a fucntion.

This is my current code:

root = tk.Tk()
root.title("START")
root.geometry("500x200+500+200")

v = tk.IntVar()
v.set(0)  # initializing the choice

my_choise = [
("Basic",1),
("Advanced",2),
('Extreme',3)
]

def ShowChoice():
    print(v.get())

tk.Label(root, 
     text="""Choose your configuration:""",
     justify = tk.LEFT,
     padx = 20).pack()

val = 0
for val, choise in enumerate(my_choise):
    tk.Radiobutton(root,text=choise,padx = 20,variable=v,command=ShowChoice,value=val).pack(anchor=tk.W)


def star_program(value):
    os.system("ifconfig")

def open_comments_file():
    os.system("gedit /home/user/Desktop/comments.txt")

def open_links_file():
    os.system("gedit /home/user/Desktop/links.txt")

frame = tk.Frame(root)
frame.pack()

open_file_c = tk.Button(frame, 
               text="Comments",
               command=open_comments_file)

open_file_f = tk.Button(frame, 
               text="Links",
               command=open_links_file)

button = tk.Button(frame, 
               text="Start",
               command=star_program(v.get()))

button.pack(side=tk.LEFT)
open_file_f.pack(side=tk.LEFT)
open_file_c.pack(side=tk.LEFT)

slogan = tk.Button(frame,
               text="Cancel",
               command=quit)
slogan.pack(side=tk.LEFT)

root.mainloop()

I would like that the buttons "Links" and "Comments" were positioned below the radiobutton, one below the other. Now, all buttons are in line, but I would like to have "start" and "cancel" at the bottom of my window.

Then I tried to use the value of the radiobutton (choice) inside the star_program function. It does not work. My idea is, based on the choice selected in the radiobutton, perform different actions when I click the button "start":

    def star_program(value):
        if value == 0:
            os.system("ifconfig")
        else:
            print "Goodbye"

In addition, concerning "start" button, I have a strange behavior. The program runs "ifconfig" command also if I don't click on "start". And If I click "start" it does not perform any action.

Any suggestion? Thanks!!!

like image 815
user3472065 Avatar asked Jan 06 '18 14:01

user3472065


People also ask

What is Tk () in Tkinter Python?

Tkinter is a Python package which comes with many functions and methods that can be used to create an application. In order to create a tkinter application, we generally create an instance of tkinter frame, i.e., Tk(). It helps to display the root window and manages all the other components of the tkinter application.

What is the use of the place () function in Tkinter Python?

The Place geometry manager is the simplest of the three general geometry managers provided in Tkinter. It allows you explicitly set the position and size of a window, either in absolute terms, or relative to another window.


1 Answers

i'm assuming this is more like what you're after:

root = tk.Tk()
root.title("START")
root.geometry("500x200+500+200")

v = tk.IntVar()
v.set(0)  # initializing the choice

my_choise = [
("Basic",1),
("Advanced",2),
('Extreme',3)
]

def ShowChoice():
    print(v.get())

tk.Label(root, 
     text="""Choose your configuration:""",
     justify = tk.LEFT,
     padx = 20).grid(column=1, row=0, sticky="nesw") # use grid instead of pack

root.grid_columnconfigure(1, weight=1)

val = 0
for val, choise in enumerate(my_choise):
    tk.Radiobutton(root,text=choise,padx = 20,variable=v,command=ShowChoice,value=val).grid(column=1, row=val+1, sticky="nw")


def star_program(value):
    os.system("ifconfig")

def open_comments_file():
    os.system("gedit /home/user/Desktop/comments.txt")

def open_links_file():
    os.system("gedit /home/user/Desktop/links.txt")

frame = tk.Frame(root)
frame.grid(column=1, row=4, sticky="nesw")

open_file_c = tk.Button(frame, 
               text="Comments",
               command=open_comments_file)

open_file_f = tk.Button(frame, 
               text="Links",
               command=open_links_file)

button = tk.Button(frame, 
               text="Start",
               command=lambda: star_program(v.get()))
# use lambda to create an anonymous function to be called when button pushed,
needed for functions where arguments are required

button.grid(column=2, row=3, sticky="nesw")
open_file_f.grid(column=1, row=1, sticky="nesw")
open_file_c.grid(column=1, row=2, sticky="nesw")

slogan = tk.Button(frame,
               text="Cancel",
               command=quit)
slogan.grid(column=4, row=3, sticky="nesw")

root.mainloop()
like image 105
James Kent Avatar answered Nov 13 '22 03:11

James Kent