Application in Tkinter 1 – Technoinc

Application in Tkinter 1 – Technoinc

Today in this blog we are going to use more buttons and canvas for drawing.

In which First we are going to see how the radio button and check buttons are implemented and how they went work in the application.

As in the previous blog, we saw the widgets to insert radio and check buttons. If you are not aware of this then go to Tkinter Widgets blog first.

So here is the code for the buttons.

First, run this code and then try to make your application.

import tkinter as tk

root = tk.Tk()

v = tk.IntVar()

v1 = tk.IntVar()

v1.set(1)

# initializing the choice, i.e. Python

languages = [

(“Python”,1),

(“Perl”,2),

(“Java”,3),

(“C++”,4),

(“C”,5)

]

gender = [(“Male”),(“Female”)]

def ShowChoice():

                        print(v1.get())

                        print(var1.get())

                        print(var2.get())

                        print(var3.get())

                        print(var4.get())

                        print(var5.get())

tk.Label(root,text=”””Choose your favourite programming language:”””,justify = tk.LEFT,padx = 20).pack()

var1 = tk.IntVar()

tk.Checkbutton(root,padx = 20, text=”Python”,

command=ShowChoice,variable=var1).pack(anchor=tk.W)

var2 = tk.IntVar()

tk.Checkbutton(root,padx = 20, text=”C++”,command=ShowChoice, variable=var2).pack(anchor=tk.W)

var3 = tk.IntVar()

tk.Checkbutton(root,padx = 20, text=”C”, command=ShowChoice,variable=var3).pack(anchor=tk.W)

var4 = tk.IntVar()

tk.Checkbutton(root, padx = 20,text=”Java”,command=ShowChoice, variable=var4).pack(anchor=tk.W)

var5 = tk.IntVar()

tk.Checkbutton(root, padx = 20,text=”Ruby”,command=ShowChoice, variable=var5).pack(anchor=tk.W)

tk.Label(root,text=”””Gender:”””,justify = tk.LEFT,padx = 20).pack()

for val, language in enumerate(gender):

tk.Radiobutton(root,text=language,padx = 20,variable=v1,command=ShowChoice,value=val).pack(anchor=tk.W)

root.mainloop()

We get this type of output after running the above code.

Here you can select zero, single or multiple programming languages because they are with check buttons but you can select only one gender from Male and Female because they are with radio buttons.

Now we are going to see different shapes in our application.

Here is one single canvas that demonstrates arc and oval.

from tkinter import Tk, Canvas, Frame, BOTH

class Example(Frame):

                        def __init__(self):

                                    super().__init__()

                                    self.initUI()

                        def initUI(self):

                                    self.master.title(“Shapes”)

                                    self.pack(fill=BOTH, expand=1)

                                    canvas = Canvas(self)

                                    canvas.create_oval(100, 100, 400, 300)

                                    canvas.create_arc(30, 200, 90, 100, start=90,extent=180)

                                    points = [10, 10, 200, 200,200, 300, 200,10]

                                    canvas.create_polygon(points)

                                    canvas.pack(fill=BOTH, expand=1)

def main():

root = Tk()

            ex = Example()

            root.geometry(“530×520+300+300”)

            root.mainloop()

if __name__ == ‘__main__’:

            main()

After running this code, you will get this output.

You can make a single shape too as per your need.

            This is not the end of the Tkinter. You can design your own application as per your needs and imagination.