w3resource

Python tkinter widgets Exercise: Create two buttons exit and hello using tkinter module

Python tkinter widgets: Exercise-3 with Solution

Write a Python GUI program to create two buttons exit and hello using tkinter module.

On pressing hello button print the text “Tkinter is easy to create GUI” on the terminal.

Sample Solution:

Python Code:

import tkinter as tk   

def write_text():
    print("Tkinter is easy to create GUI!")

parent = tk.Tk()
frame = tk.Frame(parent)
frame.pack()

text_disp= tk.Button(frame, 
                   text="Hello", 
                   command=write_text
                   )

text_disp.pack(side=tk.LEFT)

exit_button = tk.Button(frame,
                   text="Exit",
                   fg="green",
                   command=quit)
exit_button.pack(side=tk.RIGHT)

parent.mainloop()

Sample Output:

Flowchart: Create two buttons exit and hello using tkinter module

Flowchart:

Flowchart: Create two buttons exit and hello using tkinter module

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python GUI program to add a canvas in your application using tkinter module.
Next: Write a Python GUI program to create a Combobox with three options using tkinter module.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz


Python: Tips of the Day

Combines two lists into a dictionary, where the elements of the first one serve as the keys and the elements of the second one serve as the values. The values of the first list need to be unique and hashable:

Example:

def tips_to_dictionary(keys, values):
  return {key:value for key, value in zip(keys, values)}
print(tips_to_dictionary(['x', 'y'], [5, 10])) 

Output:

{'x': 5, 'y': 10}