Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python: Weight Conversion GUI using Tkinter
Next article icon

Prerequisites : Introduction to tkinter 


Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. In this article, we will learn how to create a ToDo GUI application using Tkinter, with a step-by-step guide. 


To create a tkinter : 

  • Importing the module – tkinter
  • Create the main window (container)
  • Add any number of widgets to the main window.
  • Apply the event Trigger on the widgets.

The GUI would look like below:


Let’s create a GUI based simple ToDo application in which you can add and delete the Task.
 

Below is the implementation : 

Python3
# import all functions from the tkinterfromtkinterimport*# import messagebox class from tkinterfromtkinterimportmessagebox# global list is declare for storing all the tasktasks_list=[]# global variable is declare for counting the taskcounter=1# Function for checking input error when# empty input is given in task fielddefinputError():# check for enter task field is empty or notifenterTaskField.get()=="":# show the error messagemessagebox.showerror("Input Error")return0return1# Function for clearing the contents# of task number text fielddefclear_taskNumberField():# clear the content of task number text fieldtaskNumberField.delete(0.0,END)# Function for clearing the contents# of task entry fielddefclear_taskField():# clear the content of task field entry boxenterTaskField.delete(0,END)# Function for inserting the contents# from the task entry field to the text areadefinsertTask():globalcounter# check for errorvalue=inputError()# if error occur then returnifvalue==0:return# get the task string concatenating# with new line charactercontent=enterTaskField.get()+"\n"# store task in the listtasks_list.append(content)# insert content of task entry field to the text area# add task one by one in below one by oneTextArea.insert('end -1 chars',"[ "+str(counter)+" ] "+content)# incrementedcounter+=1# function calling for deleting the content of task fieldclear_taskField()# function for deleting the specified taskdefdelete():globalcounter# handling the empty task erroriflen(tasks_list)==0:messagebox.showerror("No task")return# get the task number, which is required to deletenumber=taskNumberField.get(1.0,END)# checking for input error when# empty input in task number fieldifnumber=="\n":messagebox.showerror("input error")returnelse:task_no=int(number)# function calling for deleting the# content of task number fieldclear_taskNumberField()# deleted specified task from the listtasks_list.pop(task_no-1)# decrementedcounter-=1# whole content of text area widget is deletedTextArea.delete(1.0,END)# rewriting the task after deleting one task at a timeforiinrange(len(tasks_list)):TextArea.insert('end -1 chars',"[ "+str(i+1)+" ] "+tasks_list[i])# Driver codeif__name__=="__main__":# create a GUI windowgui=Tk()# set the background colour of GUI windowgui.configure(background="light green")# set the title of GUI windowgui.title("ToDo App")# set the configuration of GUI windowgui.geometry("250x300")# create a label : Enter Your TaskenterTask=Label(gui,text="Enter Your Task",bg="light green")# create a text entry box# for typing the taskenterTaskField=Entry(gui)# create a Submit Button and place into the root window# when user press the button, the command or# function affiliated to that button is executedSubmit=Button(gui,text="Submit",fg="Black",bg="Red",command=insertTask)# create a text area for the root# with lunida 13 font# text area is for writing the contentTextArea=Text(gui,height=5,width=25,font="lucida 13")# create a label : Delete Task NumbertaskNumber=Label(gui,text="Delete Task Number",bg="blue")taskNumberField=Text(gui,height=1,width=2,font="lucida 13")# create a Delete Button and place into the root window# when user press the button, the command or# function affiliated to that button is executed .delete=Button(gui,text="Delete",fg="Black",bg="Red",command=delete)# create a Exit Button and place into the root window# when user press the button, the command or# function affiliated to that button is executed .Exit=Button(gui,text="Exit",fg="Black",bg="Red",command=exit)# grid method is used for placing# the widgets at respective positions# in table like structure.enterTask.grid(row=0,column=2)# ipadx attributed set the entry box horizontal sizeenterTaskField.grid(row=1,column=2,ipadx=50)Submit.grid(row=2,column=2)# padx attributed provide x-axis margin# from the root window to the widget.TextArea.grid(row=3,column=2,padx=10,sticky=W)taskNumber.grid(row=4,column=2,pady=5)taskNumberField.grid(row=5,column=2)# pady attributed provide y-axis# margin from the widget.delete.grid(row=6,column=2,pady=5)Exit.grid(row=7,column=2)# start the GUIgui.mainloop()

Output: 


Create a ToDo Application using Tkinter in Python

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp