SoFunction
Updated on 2024-11-13

This is probably the most fun example of getting started with python GUI (recommended)

Simply put, GUI programming is the addition of a graphical interface to a program.

Python script development is simple, sometimes only a few lines of code can achieve rich functionality , and python itself is cross-platform , so by the programmer's favorite .

If you add a graphical interface to the program, then ordinary users will be able to use python scripts, greatly enhancing the efficiency of the work, so to add a graphical interface to the python program, write their own scripts, available to ordinary users, is indeed an exciting thing!

How do I add a GUI to a python script?

The author first considered the graphical interface to run python through the browser , in order to achieve the desired effect , python needs to use javascript to achieve some of the functions , and python needs to be installed additionally pyv8 module , I tossed a bit , and found that the pyv8 module is very troublesome to install , and rely on a lot of libraries , compile and install according to different operating systems , there are Various pitfalls, pyv8 is not suitable for ordinary users, so temporarily shelved the pyv8 module.

software interface

Then I compared pyqt5 and tkinter two modules: pyqt5 is very powerful, the interface is also beautiful, but the syntax is more complex, pyqt5 module needs to be installed separately, is not suitable for newcomers to start ;)

tkinter is python3 comes with the module, can meet the basic functional requirements, the syntax is simple, basically 5 minutes to get started, so finally chose tkinter.

Problems with current python GUI tutorials on the web.

1. The function is too simple, the basic function is to "fancy" display "Hello World".

2. Notes are not clear, copy and paste the blog code written by others, code mutilation

3. version of the old, are for python2.7 program, the way to import such asimport Tkinter , python3 should readimport tkinter

This time the author chose a "according to the ip address to locate the geographic location" of the script, as the material of this tutorial, more fun, but also relatively easy to realize:.

The explanations are in the comments, in the code.

import tkinter
import pygeoip

class FindLocation(object):
 def __init__(self):
  = ("./")
 # Create the main window that will hold the other components.
  = ()
 # Set title content for the main window
 ("global positioning (GPS)ipplacement(offline version)")
 # Create an input box and set the dimensions
 self.ip_input = (,width=30)

 # Creating a list of retrospectives
 self.display_info = (, width=50)

 # Create a button for querying results
 self.result_button = (, command = self.find_position, text = "Query")

 # Finish the layout
 def gui_arrang(self):
 self.ip_input.pack()
 self.display_info.pack()
 self.result_button.pack()

 # Find geolocation by ip
 def find_position(self):
 # Get input information
 self.ip_addr = self.ip_input.get()
 aim = .record_by_name(self.ip_addr)
 # In order to avoid illegal values, resulting in a program crash, interested in regular can write a specific rule, I in order to facilitate the understanding of newcomers, to reduce the amount of code, it is directly crude filtering
 try:

  # Get the target city
  city = aim["city"]
  # Access to target countries
  country = aim["country_name"]
  # Get the target area
  region_code = aim["region_code"]
  # Get the target longitude
  longitude = aim["longitude"]
  # Get the target latitude
  latitude = aim["latitude"]
 except:
  pass
 
 # Create temporary lists
 the_ip_info = ["Latitude:"+str(latitude),"Longitude:"+str(longitude),"Geographic code:"+str(region_code),"City:"+str(city), "Country or territory:"+str(country), "ip to query:"+str(self.ip_addr)]
 # Clear the visible part of the display list, similar to the clear command.
 for item in range(10):
  self.display_info.insert(0,"")

 # Assigning values to the display list
 for item in the_ip_info:
  self.display_info.insert(0,item)
 # Here's the return value, it's useless, it's just for looks #
 return the_ip_info


def main():
 # Initialize the object
 FL = FindLocation()
 # Make a layout
 FL.gui_arrang()
 # Main program execution
 ()
 pass


if __name__ == "__main__":
 main()

Running results (in order to better demonstrate the effect of the use of gifs, picture size is large, it is recommended to watch in a wifi environment, the tycoon at will ~).

A very sizeable demo image!

As offline query ip need global ip distribution data, so I directly choose a free offline query ip packet, in order to read the data of this packet also need to install a module:pip install pygeoip A very small number of people who installed python3 back in the day chose to install python3 without tkinter, so for the sake of learning, I'd like to add this module.pip install tkinter

If you want to convert the sample program to a windows executable file (.exe), refer to this article

https:///article/

The tutorial involves resources I have shared with you through Baidu.com, in order to facilitate the download, the resources are integrated into a separate post, the link is as follows.

/p/4f28e1ae08b1

This is the whole content of this article.