Showing posts with label tk. Show all posts
Showing posts with label tk. Show all posts

Monday, June 15, 2015

tk6. Scale in Tkinter

A scale can be used to select a number within a range. Here the range is from 1 to 10.


The number selected is printed in the label just underneath it, using the command function of the Scale.


Whenever the command is invoked by a scale change, a parameter is passed by Tkinter. It is referred to as e here, but it does not make any difference what we call it, as long as we are consistent.

# tk6.py
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.geometry('400x300')
root.title('Scale')
root['bg']='blue'
scaleVal = StringVar()
scaleVal.set('5.000')
scale = Scale(root, from_=1, to=10,
              orient = HORIZONTAL,
              command = lambda e: scaleVal.set('%.3f' % float(e)))
scale.set(5)
scale.pack(fill = X)

label = Label(root, textvariable = scaleVal, pad = 100)
label.pack(expand = True)
root.mainloop()

Output:

tk5. Using lambda functions in Tkinter

For the last example, we will now use the lambda function so we do not need a separate function.


The function is now completely inline, within the checkbutton definition.

# tk5.py
from tkinter import *
from tkinter.ttk import *

root=Tk()
root.title('Checkbutton Status')
root.geometry('300x100')
frame = Frame(root)
frame.pack()
status = StringVar()
checkbutton = Checkbutton(frame, text='status\ncheckbutton', 
     command = lambda : display.set(status.get()),
            variable=status, onvalue='on', offvalue='off')
checkbutton.grid(row = 1, column= 0)
label = Label(frame, text = 'status', pad = 5)
label.grid(row = 0, column = 1)
display = StringVar()
display.set('off')
label1 = Label(frame, textvariable = display, pad = 5)
label1.grid(row = 1, column = 1)
root.mainloop()

tk4. Checkbutton in TKinter

Checkbuttons are similar to Buttons. Now the toggling action of checking or unchecking will be the status. Whenever the state changes, we invoke the function statusChanged. This function displays the current value of the checkbutton.


We again use a 2 by 2 grid, within a Frame, with the 3 widgets being a checkbutton, static label, and non-static Label (named display).


Now, we have 2 StringVar objects, one for the checkbutton and the other for the display Label.


There is no reason to have the statusChanged function be a separate function, since it is only 1-line. We can also put it, inline, as a lambda function.

# tk4.py
from tkinter import *
from tkinter.ttk import *

def statusChanged():
    display.set(status.get())
    
root=Tk()
root.title('Checkbutton Status')
root.geometry('300x100')
frame = Frame(root)
frame.pack()
status = StringVar()
checkbutton = Checkbutton(frame, text='status\ncheckbutton', 
     command=statusChanged, variable=status,
     onvalue='on', offvalue='off')
checkbutton.grid(row = 1, column= 0)
label = Label(frame, text = 'status', pad = 5)
label.grid(row = 0, column = 1)
display = StringVar()
display.set('off')
label1 = Label(frame, textvariable = display, pad = 5)
label1.grid(row = 1, column = 1)
root.mainloop()

Output:

Sunday, June 14, 2015

tk3. Button and StringVar in Tkinter

The themed widgets from Tkinter will be used from now on, so the additional import in the code below.


The function increment, defined in the code, adds 1 to value currently held in a label.


Here, there are 3 widgets in total, within the Frame widget.


The 3 widgets take 3 of the 4 positions in a 2 by 2 grid. The button widget has text '+' and pressing it will invoke the increment function.


There are 2 labels. The first has the name label and is static with text of 'Counter'. The second, label1, can have the text updated using a StringVar object. We initialize it as '0' and update via the increment function.

# tk3.py
from tkinter import *
from tkinter.ttk import *

def increment():
    a = int(counter.get())
    counter.set(a+1)
    
root=Tk()
root.title('Button Counter')
root.geometry('200x100')
frame = Frame(root)
frame.pack()
button = Button(frame, text='+',
                    command = increment, pad = 5)
button.grid(row = 1, column= 0)
label = Label(frame, text = 'Counter', pad = 5)
label.grid(row = 0, column = 1)
counter = StringVar()
counter.set(0)
label1 = Label(frame, textvariable = counter, pad = 5)
label1.grid(row = 1, column = 1)
root.mainloop()

Output:

tk2. Labels in TKinter

A red frame fills the window.


There is one label inside the frame. Both the frame and label use pack geometry manager, rather than grid or absolute positioning with place.


The label text is 'Hello World' in big letters. We use a red background to match the frame background.

# tk2.py
from tkinter import *
root = Tk()
root.title('Welcome')
root.geometry('500x500')
frame = Frame(root)
frame['bg']='red'
frame.pack(expand = True, fill = BOTH)
label = Label(frame, text = 'Hello World!', bg = "red",
              font=("Helvetica", 64))
label.pack(side = RIGHT)
root.mainloop()

Output:

tk1. Frames in Tkinter

Tkinter is one of the GUI systems that we can use with Python, which is included in the default Python. In these examples, Python 3+ is used. For Python 2.7, you have to change the import names.


Almost any GUI application will use Frames, to embed other widgets such as Labels and Buttons. The only function of this program is to create 4 Frames in a grid arrangement.


After the import(s), we have to define an instance or the Tk class, as the root Widget. Two of its properties are then set.


For frame1 to frame4, we set three properties, width, height, and background color. We can use config function to set many properties at once.


Next, we layout the grid and start our loop.

# tk1.py
from tkinter import *
root = Tk()
root.title('The Frame')
root.geometry('500x500')
frame1 = Frame(root)
frame1['bg'] = 'red'
frame1['width'] = '240'
frame1['height'] = '240'
frame2 = Frame(root)
frame2.config(bg = 'blue', width = '240', height = '240')
frame3 = Frame(root)
frame3.config(bg = 'green', width = '240', height = '240')
frame4 = Frame(root)
frame4.config(bg = 'purple', width = '240', height = '240')
frame1.grid(row = 0, column = 0)
frame2.grid(row = 1, column = 0)
frame3.grid(row = 0, column = 1)
frame4.grid(row = 1, column = 1)
root.mainloop()

Output: