Colors
Tkinter defines color names that can be easily used as color values in Python programs. These color names are taken from the names that are provided by X Windows. Tkinter allows the use of color strings. These strings start with a number sign (#) and specify the precise values of the red, green and blue components of the color. There are three forms of color strings, depending on how many levels each component is allowed:
Fonts
Tkinter provides font descriptors that are easier to work with. The X Window System font descriptors are still supported but there are two other major kinds of font descriptors that are available in Tkinter:
1. Tuples or Strings : Font can be decribed using an n-tuple of the form:
(family, size, [option1], [option2], . . . )
The font family is in fact its name. It is a string describing the font. For example, “arial”, “courier”, or “times new roman” are all valid font families. The size is given as an integer. The remaining options are facultative, and there can be as many of them as required. The valid options are: normal, roman, bold, italic, underline and overstrike.
It is also possible to specify a font using a single string if the font name does not contain any spaces. For example, (“arial”, 10, “italic”, “bold”) is equivalent to “arial 10 italic bold”.
2. Font instances : This requires to import a separate module, named tkFont. This modules defines the Font class. An instance of this class can be used anywhere a standard Tkinter font descriptor is valid. The options that are available in the font class are listed below:
keeping every other property fixed.
Also, these fonts can be assigned to variables, which make it possible to assign the same font to multiple objects. In the eventuality that the font is modifed using config(), then all widgets sharing the font will reflect the change simultaneously. The following example demonstrates how to work with Font objects:
from tkinter import *
import tkinter.font
root =Tk()
selected_font = tkinter.font.Font(family="arial",size=20,weight=tkinter.font.BOLD)
b1 = Button(root,text="Click",font = selected_font)
b1.pack()
b2 = Button(root,text="Press",font = selected_font)
b2.pack()
selected_font.config(slant=tkinter.font.ITALIC)
root.mainloop()
The output of the program is shown below:
Variables
Tkinter has a special Variable class that all other variable subclasses inherit from. Various Tkinter widgets have options that rely on the Variable class, such as the “variable” option for Radiobuttons, the “textvariable” option for the Label, etc.
When the Tkinter variables are used for simple tasks, they do not behave differently than the regular Python variables, apart from the fact that their values have to be handled through method calls, such as get() and set(). The Variable class does not implement the get( ) method. It is only the base class
for all variables, and is not intended for direct use. Instead, each descendant of the Variable class implements its specific get( ) method. The choice behind the implementation of this master variable class is therefore not obvious. However, by taking a closer look at what the Variable class offers, the motivation behind it becomes much more apparent. Tkinter variables provide, in true Tkinter style, the possibility to add a callback which will be executed when read and write operations are performed on the variable, and also when the value associated with the variable is undefined. This allows, for example, to bind a StringVar() instance to a Label widget, which then automatically updates its caption when the value of the variable changes.
The variable options only take parameters which are instances of the Variable class (or one of its subclasses). If a regular Python variable is passed as parameter, then it will never receive the appropriate value. That is, the store and retrieve calls will silently fail. Moreover, it must be mentioned that the set() method does not do type or range checking on its parameter value.
Tkinter provides four descendants of the Variable class:
1. __init__ (self, master=None) - Constructor (an exception is raised if the master is None, since the constructor calls master.tk)
2. __ del__ (self) - Destructor
3. __ str__ (self) - Returns the name of the variable
The Variable class provides the following public methods:
1. set(self,value) - Assigns value to the variable
trace variable(self, mode, callback) - Assigns a callback to the variable. Mode must be one of ”r”, ”w”, or ”u”. Depending on the value of mode, callback is executed when a value is read from the variable, written to the variable or when its value becomes undefined, respectively. This method returns the name of the callback, which is needed to delete it.
2. trace vdelete(self, mode, cbname) - Deletes the callback with the specified name and mode.
3. trace vinfo(self) - Returns a list of all of the callback names associated with the variable and their respective modes (each mode - callback name pair is stored as a tuple).
Here I am ending today's topic, till we meet next, keep practicing and learning Python as Python is easy to learn!
Tkinter defines color names that can be easily used as color values in Python programs. These color names are taken from the names that are provided by X Windows. Tkinter allows the use of color strings. These strings start with a number sign (#) and specify the precise values of the red, green and blue components of the color. There are three forms of color strings, depending on how many levels each component is allowed:
- #RGB : 4-bit components (12 bits total), 16 levels per component.
- #RRGGBB : 8-bit components (24 bits total), 256 levels pre component.
- #RRRRGGGGBBBB : 16-bit components, (48 bits total), 65536 levels per component.
Fonts
Tkinter provides font descriptors that are easier to work with. The X Window System font descriptors are still supported but there are two other major kinds of font descriptors that are available in Tkinter:
1. Tuples or Strings : Font can be decribed using an n-tuple of the form:
(family, size, [option1], [option2], . . . )
The font family is in fact its name. It is a string describing the font. For example, “arial”, “courier”, or “times new roman” are all valid font families. The size is given as an integer. The remaining options are facultative, and there can be as many of them as required. The valid options are: normal, roman, bold, italic, underline and overstrike.
It is also possible to specify a font using a single string if the font name does not contain any spaces. For example, (“arial”, 10, “italic”, “bold”) is equivalent to “arial 10 italic bold”.
2. Font instances : This requires to import a separate module, named tkFont. This modules defines the Font class. An instance of this class can be used anywhere a standard Tkinter font descriptor is valid. The options that are available in the font class are listed below:
- family - String specifying the name of the font.
- size - Positive int for size in points, negtive int for size on pixels.
- weight - One of tkFont.BOLD or tk-Font.NORMAL
- slant - One of tkFont.ITALIC or tk-Font.NORMAL
- underline - 1 or 0
- overstrike - 1 or 0
keeping every other property fixed.
Also, these fonts can be assigned to variables, which make it possible to assign the same font to multiple objects. In the eventuality that the font is modifed using config(), then all widgets sharing the font will reflect the change simultaneously. The following example demonstrates how to work with Font objects:
from tkinter import *
import tkinter.font
root =Tk()
selected_font = tkinter.font.Font(family="arial",size=20,weight=tkinter.font.BOLD)
b1 = Button(root,text="Click",font = selected_font)
b1.pack()
b2 = Button(root,text="Press",font = selected_font)
b2.pack()
selected_font.config(slant=tkinter.font.ITALIC)
root.mainloop()
The output of the program is shown below:
Variables
Tkinter has a special Variable class that all other variable subclasses inherit from. Various Tkinter widgets have options that rely on the Variable class, such as the “variable” option for Radiobuttons, the “textvariable” option for the Label, etc.
When the Tkinter variables are used for simple tasks, they do not behave differently than the regular Python variables, apart from the fact that their values have to be handled through method calls, such as get() and set(). The Variable class does not implement the get( ) method. It is only the base class
for all variables, and is not intended for direct use. Instead, each descendant of the Variable class implements its specific get( ) method. The choice behind the implementation of this master variable class is therefore not obvious. However, by taking a closer look at what the Variable class offers, the motivation behind it becomes much more apparent. Tkinter variables provide, in true Tkinter style, the possibility to add a callback which will be executed when read and write operations are performed on the variable, and also when the value associated with the variable is undefined. This allows, for example, to bind a StringVar() instance to a Label widget, which then automatically updates its caption when the value of the variable changes.
The variable options only take parameters which are instances of the Variable class (or one of its subclasses). If a regular Python variable is passed as parameter, then it will never receive the appropriate value. That is, the store and retrieve calls will silently fail. Moreover, it must be mentioned that the set() method does not do type or range checking on its parameter value.
Tkinter provides four descendants of the Variable class:
- StringVar
- IntVar
- DoubleVar
- BooleanVar
1. __init__ (self, master=None) - Constructor (an exception is raised if the master is None, since the constructor calls master.tk)
2. __ del__ (self) - Destructor
3. __ str__ (self) - Returns the name of the variable
The Variable class provides the following public methods:
1. set(self,value) - Assigns value to the variable
trace variable(self, mode, callback) - Assigns a callback to the variable. Mode must be one of ”r”, ”w”, or ”u”. Depending on the value of mode, callback is executed when a value is read from the variable, written to the variable or when its value becomes undefined, respectively. This method returns the name of the callback, which is needed to delete it.
2. trace vdelete(self, mode, cbname) - Deletes the callback with the specified name and mode.
3. trace vinfo(self) - Returns a list of all of the callback names associated with the variable and their respective modes (each mode - callback name pair is stored as a tuple).
Here I am ending today's topic, till we meet next, keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment