In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to control control properties through OptionDB in Tkinter". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
We have basically given a brief description of each of the basic controls in Tkinter. In these sample code, the user can control the foreground color, background color, font and other properties of each control through properties. This approach is intuitive and easy to understand, but there are several disadvantages:
Even if you want the same settings, each control needs to be developed separately.
The setting information is submerged in the code, and the code needs to be modified whether it is to add or subtract or adjust the attributes. Needless to say, it is possible to break the integrity of the code.
Both of these issues can be solved through OptionDB provided by Tkinter. Next, we take the calculator Mini Program as an example to illustrate its usage. The initial state of this program is as follows:
First, Tkinter provides a way for option_add method users to define control properties according to classification. For example, for two label controls that represent evaluation results and expressions, you can use the following code to specify their background color:
Root.option_add ('* Label.background', "# a0ffa0")
The meaning of'* Label.background' in the code is the background color of all Label controls.
If you need to control each control individually, you must first specify a name for the control. The following code specifies the respective names of the two label controls through the name property.
# create a label to change state.upper_display = Label (root, name='upperDisplay', foreground= "# 000000", width=24, anchor=E) upper_display.grid (row=1, column=0, columnspan=4, sticky=E + W)
# create a label to change state.lower_display = Label (root, name='lowerDisplay', foreground= "# 000000", width=12, anchor=E) lower_display.grid (row=2, column=0, columnspan=4, sticky=E + W)
After the name assignment is complete, you can use the name to specify properties for a specific control. In order to distinguish from the classification specification, the name of the control must be lowercase.
FtTimes1 = Font (family='Times', size=12, weight=BOLD) ftTimes2 = Font (family='Times', size=24, weight=BOLD) root.option_add ('* upperDisplay.font', ftTimes1) root.option_add ('* lowerDisplay.font', ftTimes2) root.option_add ('* Button*font', ftTimes1)
In addition to the two tags, the code also assigns fonts to all the buttons, and the result is as follows:
As you can see from the above example, by using OptionDB, you can specify properties for multiple controls at the same time with very little code. In addition, Tkinter provides the ability to modify control properties through configuration files. The following code is an example of specifying the font and color of each control in the calculator.
* Label.background:#a0ffa0*upperDisplay.font:times 12 bold*lowerDisplay.font:times 24 bold*Button*font: Meiryo 12 bold*Button*foreground:#007700*ckey*foreground:#ff0000*cekey*foreground:#ff0000*back*foreground:#ff0000*devkey*foreground:#0000ff*mulkey*foreground:#0000ff*minukey*foreground:#0000ff*pluskey*foreground:#0000ff*equalkey*foreground:#000000
After the configuration file is complete, save it with an appropriate name. 26 OpDb.txt is used in this example.
The final step is to add the following line to the code to read in the configuration file:
Root.option_readfile ('26 OpDb.txt')
The execution result of the program is as follows:
This is the end of the content of "how to control control properties through OptionDB in Tkinter". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.