📜  Tkinter 简介

📅  最后修改于: 2022-05-13 01:54:24.465000             🧑  作者: Mango

Tkinter 简介

图形用户界面 (GUI)是一种用户界面形式,它允许用户使用图标、菜单、窗口等项目通过视觉指示器与计算机进行交互。它比用户与计算机交互的命令行界面 (CLI) 具有优势通过仅使用键盘编写命令并且其使用比 GUI 更困难。

什么是 Tkinter?

Tkinter是用于创建 GUI 应用程序的内置Python模块。它是在Python中创建 GUI 应用程序最常用的模块之一,因为它简单易用。您无需担心单独安装 Tkinter 模块,因为它已经随Python提供。它为 Tk GUI 工具包提供了一个面向对象的接口。其他一些可用于创建我们自己的 GUI 应用程序的Python库是

  • 基维
  • Python Qt
  • wxPython

      在所有 Tkinter 中使用最广泛

      什么是小部件?

      Tkinter 中的小部件是 GUI 应用程序的元素,它为用户提供各种控件(如标签、按钮、组合框、复选框、菜单栏、单选按钮等)以与应用程序交互。

      tkinter 程序的基本结构基本的 Tkinter 小部件:

      WidgetsDescription
      LabelIt is used to display text or image on the screen
      ButtonIt is used to add buttons to your application
      CanvasIt is used to draw pictures and others layouts like texts, graphics etc.
      ComboBoxIt contains a down arrow to select from list of available options
      CheckButtonIt displays a number of options to the user as toggle buttons from which user can select any number of options.
      RadiButtonIt is used to implement one-of-many selection as it allows only one option to be selected
      EntryIt is used to input single line text entry from user
      FrameIt is used as container to hold and organize the widgets
      MessageIt works same as that of label and refers to multi-line and non-editable text
      ScaleIt is used to provide a graphical slider which allows to select any value from that scale
      ScrollbarIt is used to scroll down the contents. It provides a slide controller.
      SpinBoxIt is allows user to select from given set of values
      TextIt allows user to edit multiline text and format the way it has to be displayed
      MenuIt is used to create all kinds of menu used by an application

        例子

        from tkinter import * 
        from tkinter.ttk import *
            
        # writing code needs to
        # create the main window of 
        # the application creating 
        # main window object named root
        root = Tk()
          
        # giving title to the main window
        root.title("First_Program")
          
        # Label is what output will be 
        # show on the window
        label = Label(root, text ="Hello World !").pack()
          
        # calling mainloop method which is used
        # when your application is ready to run
        # and it tells the code to keep displaying 
        root.mainloop()
        

        输出