Tkinter Checkbutton, Radiobutton & Entry

 

Tkinter Checkbutton 

Sintax :

w = checkbutton(master, options)


Contoh :

from tkinter import *   

top = Tk()  

top.geometry("200x200")  

checkvar1 = IntVar()  

checkvar2 = IntVar()  

checkvar3 = IntVar()  

chkbtn1 = Checkbutton(top, text = "C", variable = checkvar1, onvalue = 1, offvalue = 0, height = 2, width = 10)  

chkbtn2 = Checkbutton(top, text = "C++", variable = checkvar2, onvalue = 1, offvalue = 0, height = 2, width = 10)  

chkbtn3 = Checkbutton(top, text = "Java", variable = checkvar3, onvalue = 1, offvalue = 0, height = 2, width = 10)  

chkbtn1.pack()  

chkbtn2.pack()  

chkbtn3.pack()  

top.mainloop()  



Tkinter Radiobutton

Sintax :

w = Radiobutton(top, options)


Contoh :

from tkinter import *  

def selection():  

   selection = "You selected the option " + str(radio.get())  

   label.config(text = selection)  

top = Tk()  

top.geometry("300x150")  

radio = IntVar()  

lbl = Label(text = "Favourite programming language:")  

lbl.pack()  

R1 = Radiobutton(top, text="C", variable=radio, value=1,  

                  command=selection)  

R1.pack( anchor = W )  

R2 = Radiobutton(top, text="C++", variable=radio, value=2,  

                  command=selection)  

R2.pack( anchor = W )  

R3 = Radiobutton(top, text="Java", variable=radio, value=3,  

                  command=selection)  

R3.pack( anchor = W)  

label = Label(top)  

label.pack()  

top.mainloop()


Tkinter Entry

Sinax :

w = Entry (parent, options)


Contoh 1 :

# !/usr/bin/python3  

from tkinter import *  

top = Tk()  

top.geometry("400x250")  

name = Label(top, text = "Name").place(x = 30,y = 50)  

email = Label(top, text = "Email").place(x = 30, y = 90)  

password = Label(top, text = "Password").place(x = 30, y = 130)  

sbmitbtn = Button(top, text = "Submit",activebackground = "pink", activeforeground = "blue").place(x = 30, y = 170)  

e1 = Entry(top).place(x = 80, y = 50)  

e2 = Entry(top).place(x = 80, y = 90)  

e3 = Entry(top).place(x = 95, y = 130)  

top.mainloop() 



Contoh 2 :

import tkinter as tk  

from functools import partial  

def call_result(label_result, n1, n2):  

    num1 = (n1.get())  

    num2 = (n2.get())  

    result = int(num1)+int(num2)  

    label_result.config(text="Result = %d" % result)  

    return  

root = tk.Tk()  

root.geometry('400x200+100+200')  

root.title('Calculator')  

number1 = tk.StringVar()  

number2 = tk.StringVar()  

labelNum1 = tk.Label(root, text="A").grid(row=1, column=0)  

labelNum2 = tk.Label(root, text="B").grid(row=2, column=0)  

labelResult = tk.Label(root)  

labelResult.grid(row=7, column=2)  

entryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2)  

entryNum2 = tk.Entry(root, textvariable=number2).grid(row=2, column=2)  

call_result = partial(call_result, labelResult, number1, number2)  

buttonCal = tk.Button(root, text="Calculate", command=call_result).grid(row=3, column=0)  

root.mainloop()