Notes (1) Basic small grammar of python

1. The difference between import and from import in python

Four ways to import modules in python:

import xx   # 直接使用import进行导入

import xx as yy   # 例子:import numpy as np,再次引用时直接使用np应用即可


from xx import yy    
# xx表示模块,yy表示xx模块的特定的类、函数、变量等,就是从xx模块中引用yy的类、函数、变量等

from xx import *     # *表示xx模块中的所有具体的类、函数、变量等

The difference between the two (import...|from ... import ...):

The first is to import a module and execute the module again. To import variables in the current namespace, you need to use the variables, functions, classes, etc. in the imported module through xx.yy; the
second is to use the module The variable yy in is imported into the current namespace, and it can be called directly with yy when using it. Note that there should be no duplicate names in the program. Remember here that from xx import * is not used as much as possible, because it will destroy the management of the namespace, and when using from xx import *, you cannot import protected attributes that start with a single underscore and private attributes that start with a double underscore. Attributes.
 

Two, tkinter library introduction

from tkinter import *             # 使用tkinter制作了一个简单的验证登陆窗口
from tkinter.messagebox import *  # messagebox-弹窗库(弹窗提醒)

class Login(object):   # 定义Login类
    def __init__(self, master=None):
        self.root = master                         # 定义内部变量root
        self.root.geometry('%dx%d' % (600, 400))   # geometry-设置窗口大小
        self.root.resizable(0, 0)                  # resizable-窗口大小固定
        self.username = StringVar()
        self.password = StringVar()
        # 利用StringVar接收用户输入, 制作用户名和密码的验证登录窗口
        self.cratePage()

.grid() method

The following statement can display the w  plug- in in your application interface  :
w.grid(option=value , ...)
This method uses the grid structure manager to register the  w  plug-in. Without this statement, the plug-in will not be displayed on the screen, but only in the code. The option item in the statement can be viewed in Table 1, the parameters of the .grid() structure manager.

Table 1. Parameters of the .grid() structure manager

option Explanation
column The column value of the plug-in deployment, starting from 0. The default value is 0
columnspan Under normal circumstances, a plug-in only occupies one unit  . However, you can  merge multiple adjacent cells in a row  by setting  columnspan  , and use this  cell to  place this plugin. For example,   this will place the plug-in  w  in the unit after merging columns 2, 3, and 4 in row 0   .w.grid(row=0, column=2, columnspan=3)
in_ Use  the child plug- in that  in_=w2 can   register w as  w2w2  must be   a child plugin that specifies the parent plugin when w is created.
ipadx Internal padding in the x direction. Inside the plug-in, the left and right directions fill the space of the specified length.
ipads The inner padding in the y direction. Inside the plug-in, the space of the specified length is filled up and down.
padx The outer padding in the x direction. Outside the plug-in, the left and right directions are filled with the specified length of space.
pads The inner padding in the y direction. Inside the plug-in, the space of the specified length is filled up and down.
row The row value of the plug-in deployment, starting from 0. The default value is the next value that is not released.
rowspan Under normal circumstances, a plug-in only occupies one unit  . However, you can  merge multiple adjacent cells in a column  by setting  rowspan  , and use this  cell to  place this plugin. For example,   this will  arrange the plug-in  w in a combined  area of 20  units , that is, 3-6 rows and 2-6 columns.w.grid(row=3, column=2, rowspan=4, columnspan=3)
sticky This parameter is used to determine: how to allocate the  extra space in the unit under the normal size of the plug-  in. details as follows.
  • If the sticky attribute is not declared, the plugin will be centered in the cell  by default  .
  • By setting  sticky=tk.NE (to the top right), sticky=tk.SE(to the bottom right), sticky=tk.SW(to the bottom left), sticky=tk.NW(to the top left), the plug-in can be placed in   a corner of the unit .
  • By setting  sticky=tk.N(to the top), sticky=tk.E(to the right), sticky=tk.S(to the bottom), sticky=tk.W(to the left), the plug-in can be arranged in   a certain direction of the unit .
  • By setting sticky=tk.N+tk.S, extend the plug-in in the vertical direction and keep it horizontally centered.
  • By setting sticky=tk.E+tk.W, extend the plug-in in the horizontal direction and keep it vertically centered.
  • By setting sticky=tk.N+tk.E+tk.W, extend the plug-in in the horizontal and vertical directions to fill the  unit  .
  • Other combinations can also be used. For example, sticky=tk.N+tk.S+tk.Wextend the plug-in in the vertical direction and place it to the left.

Note: The .grid() method is translated from the New Mexico Institute of Technology Computer Center

 

Third, the definition and use of class in python

Class : Used to describe a collection of objects with the same properties and methods. It defines the attributes and methods common to each object in the collection .
Object: It is an instantiation of a class.                   Method: The function defined in the class.

The composition of the class
name Class name
Attributes Refers to the characteristics of the object (a set of data)
method The method that allows the object to operate (behavior/function)

Python 3.0 and above have cancelled the classic classes, and they are all new-style classes by default (the syntax of the new-style classes -> class class name (object): ).

class MainPage(object): 

The class object supports two operations: attribute reference and instantiation.
Syntax of attribute reference: obj. attribute Syntax of
class instantiation: obj = class name ()
method invocation of class: obj. method name ()

# 举例说明
class teacher(object):
    def speak(self):  # 定义方法
        print("%s 说:同学们,还有%s天你们就要毕业了!" % (self.name, self.day))


T = teacher()  # 转换为实例对象
T.name = "张老师"  # 给对象添加属性
T.day = "300"

T.speak()
# >>>张老师 说:同学们,还有300天你们就要毕业了!

Four, python small grammar notes

1. Introduction to the def __init__(self) method:

In the __init__ method, there is only one self, which refers to the instance itself, but in the class part of the method, multiple attributes can be included.

class Student:
    def __init__(self):
        self.name = None
        self.age = None
        self.sex = None

 

2、python——None

None, Which means empty in python . It is a type and an object. Of course, although the None value is not False, its meaning is equivalent to empty, and the bool value is False.

 

3. Introduction to Python formatted output %s and %d

Python formatted output of %s and %d and related introduction of%
%s Indicates that an object in the formatting format is a character, and prints a string
%d Print integer
%character Mark the beginning of the conversion specifier, place a string (formatting string) on ​​the left side of %, and place the value you want to format on the right side.
%f
Print floating point number

 

 

 

 

 

for example

# 格式化输出实例说明
# 1、打印字符串——%s
print("My name is %s ." % "Tom")
# >>> My name is Tom .

# 2、打印整数——%d
print("My age is %d ." % 2)
# >>> My age is 20 .

# 3、打印浮点数——%f
print("My height is %f m" % 1.8)
# >>> My height is 1.800000 m

# 4.打印浮点数(指定保留小数点位数)——%.2f
print("My height is %.2f m" % 1.8)
# >>> My height is 1.80 m

 

Guess you like

Origin blog.csdn.net/weixin_44940488/article/details/106315780