In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of the sample analysis of GUI programming in python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
To put it simply, GUI programming is to add a graphical interface to the program.
Python script development is simple, sometimes only a few lines of code can achieve rich functions, and python itself is cross-platform, so it is popular with programmers.
If you add a graphical interface to the program, then ordinary users can also use python scripts, which greatly improves work efficiency, so adding a graphical interface to python programs and providing their own scripts to ordinary users is indeed an exciting thing!
How to add a graphical interface to a python script?
First of all, the author considers the graphical interface of running python through the browser. In order to achieve the ideal effect, python needs to achieve some functions with javascript, and python needs to install additional pyv8 module. I toss about and find that pyv8 module installation is very troublesome, and depends on a lot of libraries, compilation and installation also according to different operating systems, there are various pits, pyv8 is not suitable for ordinary users, so I temporarily shelved the pyv8 module.
Software interface
Then I compared the two modules of pyqt5 and tkinter: pyqt5 is very powerful and the interface is beautiful, but the syntax is complicated, and the pyqt5 module needs to be installed separately, which is not suitable for beginners.
Tkinter is a module that comes with python3, which can meet the basic functional requirements, and the syntax is simple, so it can be used in 5 minutes, so I finally chose tkinter.
The problems with the current online python GUI tutorials:
1. The function is too simple, the basic function is "fancy" display "Hello World"
two。 The comment is not clear, copy and paste the blog code written by others, the code is incomplete
3. The version is old and is aimed at python2.7 programs. Import methods such as import Tkinter and python3 should be import tkinter.
This time, the author chose a script of "locating geographical location according to ip address" as the material for this tutorial, which is more fun and easier to implement:
All the explanations are put in the comments and put on the code:
Import tkinterimport pygeoipclass FindLocation (object): def _ init__ (self): self.gi = pygeoip.GeoIP (". / GeoLiteCity.dat") # create the main window to accommodate other components self.root = tkinter.Tk () # set the title content of the main window self.root.title ("Global Positioning ip location (offline version)") # create an input box and set the size self.ip_input = tkinter.Entry (self.root Width=30) # create an echo list self.display_info = tkinter.Listbox (self.root, width=50) # create a button for query results self.result_button = tkinter.Button (self.root, command = self.find_position) Text = "query") # complete layout def gui_arrang (self): self.ip_input.pack () self.display_info.pack () self.result_button.pack () # find geographic location def find_position (self) according to ip: # get input information self.ip_addr = self.ip_input.get () aim = self.gi.record_by_name (self.ip_addr) # to avoid illegal values, causing the program to crash If you are interested, you can write specific rules with rules. I reduce the amount of code in order to make it easier for beginners to understand. Directly and extensively filter try: # get target city city = aim ["city"] # get target country country = aim ["country_name"] # get target area region_code = aim ["region_code"] # get target longitude longitude = aim ["longitude"] # get target latitude latitude = aim ["latitude"] except: pass # create a temporary list The_ip_info = ["Latitude:" + str (latitude) "Longitude:" + str (longitude), "region code:" + str (region_code), "city:" + str (city), "country or region:" + str (country), "ip to be queried:" + str (self.ip_addr)] # clear the visible part of the echo list, similar to the clear command for item in range (10): self.display_info.insert (0) "") # assign a value to the echo list for item in the_ip_info: self.display_info.insert (0 main__ item) # the return value here is useless, just to look good return the_ip_infodef main (): # initialize object FL = FindLocation () # layout FL.gui_arrang () # main program executes tkinter.mainloop () passif _ name__ = = "_ _ main__": main ()
Running effect (for a better demonstration effect, the gif diagram is used. The image size is large. It is recommended to view it in the wifi environment. Tuhao is free to watch it.
A demonstration picture with a lot of size!
Because offline query ip needs global IP distribution data, so I directly selected a free offline query ip data package, in order to read the data of this package also need to install a module: pip install pygeoip, very few people installed python3 that year, chose the python3 installation package without tkinter, in order to learn, or to add this module: pip install tkinter
Thank you for reading! This is the end of this article on "sample Analysis of GUI programming in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.