python Implementation GUI of Graphical User Interface Programming Detailed Explanation

  • 2021-07-18 08:41:44
  • OfStack

Python supports a variety of third-party libraries for graphical interfaces, including:

wxWidgets

Qt

GTK

Tkinter: The Tkinter module (Tk interface) is the interface to Python's standard Tk GUI toolkit. Tk and Tkinter can be used on most Unix platforms, as well as Windows and Macintosh systems. Subsequent versions of Tk 8.0 implement a native window style and work well on most platforms.

wxPython: wxPython is an open source software, an excellent GUI graphics library of Python language, which allows Python programmers to easily create a complete GUI user interface with full function keys.

Jython: The Jython program integrates seamlessly with Java. Except for one standard module, Jython uses the modules of Java. Jython has almost all the modules in the standard Python that do not depend on the C language. For example, the user interface for Jython will use Swing, AWT, or SWT. Jython can be compiled dynamically or statically into Java bytecode.

Tkinter

Let's sort out one concept:

The Python code we wrote will call the built-in Tkinter, and Tkinter encapsulates the interface to access Tk;

Tk is a graphics library, supporting multiple operating systems, using Tcl language development;

Tk invokes the native GUI interface provided by the operating system to complete the final GUI.

Therefore, our code only needs to call the interface provided by Tkinter.

In GUI, each Button, Label, input box, etc. is an Widget. Frame is an Widget that can accommodate other Widget, and all Widget are combined into a tree.

The pack () method adds Widget to the parent container and implements the layout. pack () is the simplest layout, and grid () enables more complex layouts.

Tkinter

Create an GUI program

1. Import Tkinter module

2. Create controls

3. Specify the master of this control, that is, which 1 control belongs to

4. Tell GM (geometry manager) that a control has been generated.

Example:


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import Tkinter
top = Tkinter.Tk()
#  Enter the message loop 
top.mainloop()

Component

Tkinter provides a variety of controls such as buttons, labels, and text boxes that are used in an GUI application. These controls are often referred to as controls or parts.

At present, there are 15 kinds of Tkinter components. We present these components as well as a brief introduction in the following table:

控件 描述
Button 按钮控件;在程序中显示按钮。
Canvas 画布控件;显示图形元素如线条或文本
Checkbutton 多选框控件;用于在程序中提供多项选择框
Entry 输入控件;用于显示简单的文本内容
Frame 框架控件;在屏幕上显示1个矩形区域,多用来作为容器 *****************************
Label 标签控件;可以显示文本和位图
Listbox 列表框控件;在Listbox窗口小部件是用来显示1个字符串列表给用户
Menubutton 菜单按钮控件,由于显示菜单项。
Menu 菜单控件;显示菜单栏,下拉菜单和弹出菜单
Message 消息控件;用来显示多行文本,与label比较类似
Radiobutton 单选按钮控件;显示1个单选的按钮状态
Scale 范围控件;显示1个数值刻度,为输出限定范围的数字区间
Scrollbar 滚动条控件,当内容超过可视化区域时使用,如列表框。.
Text 文本控件;用于显示多行文本
Toplevel 容器控件;用来提供1个单独的对话框,和Frame比较类似
Spinbox 输入控件;与Entry类似,但是可以指定输入范围值
PanedWindow PanedWindow是1个窗口布局管理的插件,可以包含1个或者多个子控件。
LabelFrame labelframe 是1个简单的容器控件。常用与复杂的窗口布局。
tkMessageBox 用于显示你应用程序的消息框。

Standard attribute

Standard properties are properties common to all controls, such as size, font, color and so on.

属性 描述
Dimension 控件大小;
Color 控件颜色;
Font 控件字体;
Anchor 锚点;
Relief 控件样式;
Bitmap 位图;
Cursor 光标;

Geometric management

Tkinter control has a specific geometric state management method, which manages the whole control area organization. Under 1 is the geometric management class disclosed by Tkinter: package, grid and location

几何方法 描述
pack() 包装;
grid() 网格;
place() 位置;

#!/usr/bin/env python

import os
from time import sleep
from Tkinter import *

class DirList(object):
  def __init__(self, initdir=None):
    self.top = Tk()
    self.label = Label(self.top,
      text='Directory Lister v1.2')
    self.label.pack()

    self.cwd=StringVar(self.top)

    self.dirl = Label(self.top, fg='blue',
      font=('Helvetica', 12, 'bold'))
    self.dirl.pack()

    self.dirfm = Frame(self.top)
    self.dirsb = Scrollbar(self.dirfm)
    self.dirsb.pack(side=RIGHT, fill=Y)
    self.dirs = Listbox(self.dirfm, height=15,
      width=50, yscrollcommand=self.dirsb.set)
    self.dirs.bind('<Double-1>', self.setdirandgo)
    self.dirsb.config(command=self.dirs.yview)
    self.dirs.pack(side=LEFT, fill=BOTH)
    self.dirfm.pack()

    self.dirn = Entry(self.top, width=50,
      textvariable=self.cwd)
    self.dirn.bind('<Return>', self.dols)
    self.dirn.pack()

    self.bfm = Frame(self.top)
    self.clr = Button(self.bfm, text='Clear',
      command=self.clrdir,
      activeforeground='white',
      activebackground='blue')
    self.ls = Button(self.bfm,
      text='List Directory',
      command=self.dols,
      activeforeground='white',
      activebackground='green')
    self.quit = Button(self.bfm, text='Quit',
      command=self.top.quit,
      activeforeground='white',
      activebackground='red')
    self.clr.pack(side=LEFT)
    self.ls.pack(side=LEFT)
    self.quit.pack(side=LEFT)
    self.bfm.pack()

    if initdir:
      self.cwd.set(os.curdir)
      self.dols()

  def clrdir(self, ev=None):
    self.cwd.set('')

  def setdirandgo(self, ev=None):
    self.last = self.cwd.get()
    self.dirs.config(selectbackground='red')
    check = self.dirs.get(self.dirs.curselection())
    if not check:
      check = os.curdir
    self.cwd.set(check)
    self.dols()

  def dols(self, ev=None):
    error = ''
    tdir = self.cwd.get()
    if not tdir:
      tdir = os.curdir

    if not os.path.exists(tdir):
      error = tdir + ': no such file'
    elif not os.path.isdir(tdir):
      error = tdir + ': not a directory'

    if error:
      self.cwd.set(error)
      self.top.update()
      sleep(2)
      if not (hasattr(self, 'last') \
        and self.last):
          self.last = os.curdir
      self.cwd.set(self.last)
      self.dirs.config(
        selectbackground='LightSkyBlue')
      self.top.update()
      return

    self.cwd.set(
      'FETCHING DIRECTORY CONTENTS...')
    self.top.update()
    dirlist = os.listdir(tdir)
    dirlist.sort()
    os.chdir(tdir)
    self.dirl.config(text=os.getcwd())
    self.dirs.delete(0, END)
    self.dirs.insert(END, os.curdir)
    self.dirs.insert(END, os.pardir)
    for eachFile in dirlist:
      self.dirs.insert(END, eachFile)
    self.cwd.set(os.curdir)
    self.dirs.config(
      selectbackground='LightSkyBlue')

def main():
  d = DirList(os.curdir)
  mainloop()

if __name__ == '__main__':
  main()

Related articles: