15AH, San Francisco

California, United States.

Send Your Mail At:

tianyingkejishe@sina.cn

Working Hours

Mon-Sat: 9.30am To 7.00pm

分类标题

Autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et dolore feugait.

分类归档 编程语言

Python=Lable和LableFrame应用

import os
import tkinter as tk
from tkinter import *

master = tk.Tk()
entry1_str = tk.StringVar()

def helloCallBack():
    entry1_str.set("好的回购好的回购好的回购好的回购好的回购好的回购")
    print("hello word")
    
def gui():
    master.title("Lable家族")
    master.geometry("800x480")
    master.update()

    theLableFrame = tk.LabelFrame(master,text="文件列表",padx=5, pady=5,bd=5)
    theLableFrame.pack(fill=BOTH, expand=YES, padx=10, pady=10)
    
    theLable = tk.Label(theLableFrame,width = master.winfo_width(),wraplength = 80,anchor=NW,textvariable=entry1_str)
    theLable.pack(fill=BOTH, expand=YES, padx=10, pady=10)
    
    theButton = tk.Button(master, text ="点我", command = helloCallBack)
    theButton.pack(side='bottom')
    
    master.mainloop()


if __name__ == '__main__':
    gui()
    

实现效果图如下:

python import引入不同路径下的模块

python 包含子目录中的模块方法比较简单,关键是能够在sys.path里面找到通向模块文件的路径。
下面将具体介绍几种常用情况:
(1)主程序与模块程序在同一目录下:
如下面程序结构:
`– src
|– mod1.py
`– test1.py
若在程序test1.py中导入模块mod1, 则直接使用 import mod1或from mod1 import *;

(2)主程序所在目录是模块所在目录的父(或祖辈)目录
如下面程序结构:
`– src
|– mod1.py
|– mod2
| `– mod2.py
`– test1.py
若在程序test1.py中导入模块mod2, 需要在mod2文件夹中建立空文件__init__.py文件(也可以在该文件中自定义输出模块接口); 然后使用 from mod2.mod2 import * 或import mod2.mod2.

(3)主程序导入上层目录中模块或其他目录(平级)下的模块
如下面程序结构:
`– src
|– mod1.py
|– mod2
| `– mod2.py
|– sub
| `– test2.py
`– test1.py
若在程序test2.py中导入模块mod1和mod2。首先需要在mod2下建立__init__.py文件(同(2)),src下不必建立该文件。然后调用方式如下:
下面程序执行方式均在程序文件所在目录下执行,如test2.py是在cd sub;之后执行python test2.py
而test1.py是在cd src;之后执行python test1.py; 不保证在src目录下执行python sub/test2.py成功。
import sys
sys.path.append(“..”)
import mod1
import mod2.mod2

(4)从(3)可以看出,导入模块关键是能够根据sys.path环境变量的值,找到具体模块的路径。这里仅介绍上面三种简单情况。

Python3异常-AttributeError: module ‘sys’ has no attribute ‘setdefaultencoding

基于python3.6.1版本,在一个.py文件中,加入这3行:
import requests, re, sys
reload(sys)
sys.setdefaultencoding(“utf-8”)
出现这样的错误:
sys.setdefaultencoding(“utf-8”)
AttributeError: module ‘sys’ has no attribute ‘setdefaultencoding’
原因分析:
Python3字符串默认编码unicode, 所以sys.setdefaultencoding也不存在了
解决:
去掉,sys.setdefaultencoding

PyQt5 AttributeError: ‘QGridLayout’ object has no attribute ‘setMargin’

PyQt4和PyQt5版本差异。

PyQt4采用:QVBoxLayout的setMargin方法

PyQt5采用:QVBoxLayout的setContentsMargins方法

 

错误提示: return QtGui.QApplication.translate(context, text, disambig)
AttributeError: module ‘PyQt5.QtGui’ has no attribute ‘QApplication’

修改:return QApplication.translate(context, text, disambig)

原因:PyQt4和PyQt5版本差异。

 

错误提示:NameError: name ‘unicode’ is not defined

修改:Python2 的unicode 函数在 Python3 中被命名为 str。在 Python3 中使用 ·str 来代替 Python2 中的 unicode.

原因:There is no such name in Python 3, no. You are trying to run Python 2 code in Python 3. In Python 3, unicode has been renamed to str.

AttributeError: module ‘pymysql’ has no attribute ‘connect’

这里解释为文件名字和pymysql module名字重复了。麻蛋,原来还这么弱智。

Error

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='pymysql')
AttributeError: 'module' object has no attribute 'connect'
code

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='pymysql')

cur = conn.cursor()

cur.execute("SELECT Host,User FROM user")

print(cur.description)

print()

for row in cur:
   print(row)

cur.close()
conn.close()