web

.
'''

sudo apt-get install python3-pyqt5
#打开一个网页
import
用默认浏览器打开一个网页
webbrowser.open('http://www.baidu.com')
import sys
#导入PyQt5的包
from PyQt5.QtCore import QUrl
#导入窗口
from PyQt5.QtWidgets import QApplication
#视图引擎
from PyQt5.QtWebEngineWidgets import QWebEnginePage ,QWebEngineView
#创建一个对象
app = QApplication(sys.argv)
#创建一个视图对象
browser = QWebEngineView()
#
browser.load(QUrl("http://www.baidu.com"))
browser.show()

app.exec_()
#PyQt5
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QDesktopWidget, QLabel, QGridLayout
#网页浏览器 webbrowser
import webbrowser, sys

#自定义类
class Ui_MainWindow(QWidget):
#设置标题
item_name = "PyQt打开外部链接"

def __init__(self):
#初始化父类
super().__init__()
self.initUI()

def initUI(self):
#初始化自己
self.tips_1 = QLabel("网站:<a href='http://www.baidu.com/'>http://www.baidu.com/</a>");
self.tips_1.setOpenExternalLinks(True)
#一个按钮
self.btn_webbrowser = QPushButton('webbrowser效果', self)
#连接信号
self.btn_webbrowser.clicked.connect(self.btn_webbrowser_Clicked)
#添加一个布局器
grid = QGridLayout()
grid.setSpacing(10)

grid.addWidget(self.btn_webbrowser, 1, 0)
grid.addWidget(self.tips_1, 2, 0)

self.setLayout(grid)

self.resize(250, 150)
self.setMinimumSize(266, 304);
self.setMaximumSize(266, 304);
self.center()
self.setWindowTitle(self.item_name)
self.show()

def btn_webbrowser_Clicked(self):
webbrowser.open('http://www.baidu.com/')
#
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
#格式控制

if __name__ == "__main__":
app = QApplication(sys.argv)
a = Ui_MainWindow()
sys.exit(app.exec_())

'''

'''
Tkinter模块 (kt接口) 是图形界面开发的库
'''
'''
import tkinter as tk

from tkinter import messagebox
#数据存储 pickle 可以将序列化对象保存到磁盘 并在需要的时候读出来 任何对象都可以执行序列化炒操作
import pickle
import webbrowser
#创建一个窗口
window = tk.Tk()
# welcome Image
window.title('selen')
window.maxsize(400,300)
window.minsize(400,300)
window.geometry('+450+300')
#创建canvas 画布
canvas = tk.Canvas(window,height = 200,width = 500)
#图形文件 转化为 image对象
image_file = tk.PhotoImage(file='HQ.png')
image = canvas.create_image(180,20,anchor='nw',image = image_file)
canvas.pack(side = 'top')

#user information
#创建两个lable
tk.Label(window,text= 'user name: ').place(x=120,y=150)
tk.Label(window,text='password:').place(x=120,y = 190)
#声明两个变量 保存 name passwd
#用户名
var_user_name = tk.StringVar()
var_user_name.set('selen')
#entry using to scanf single row string
entry_user_name = tk.Entry(window,textvariable = var_user_name)
entry_user_name.place(x=200,y=150)
var_user_pwd = tk.StringVar()
var_user_pwd.set('selen')
entry_user_pwd=tk.Entry(window,textvariable=var_user_pwd,show = '*')
entry_user_pwd.place(x=200,y=190)

def usr_login():
#登录
user_name = var_user_name.get()
user_pwd = var_user_pwd.get()
try:
with open('user_into.pickle','rb') as user_file:
user_info = pickle.loads(user_file)
except FileNotFoundError:
with open("user_info.pickle","wb") as e_user_file:
user_into={'admin':'admin'}
pickle.dump(user_info,e_user_file)
if user_name in user_info:
if user_pwd == user_info[user_name]:
webbrowser.open('http://www.baidu.com/')
messagebox.showinfo(title = 'Welcom',message='How are you'+user_name)
else:

messagebox.showerror(message="Error ,your passwd is wrong try again")
else:
is_sign_up = messagebox.askyesno('Welcome','you have not signed uo yet,Sign up now?')
if is_sign_up:
usr_sign_up()
#注册函数
def usr_sign_up():

def sign_to_Mofan_Python():
#获得三条数据
np = new_pwd.get() #新密码
npf = new_pwd_confirm.get() #确认密码
nn = new_name.get() #新用户名
with open('usr_info.pickle','rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
if np!=npf:
tk.messagebox.showerror('Error ','Password and confirm passwd must be the same')
elif nn in exist_usr_info:
tk.messagebox('Error','the user has sign up already ')
else:
print(type(exist_usr_info))
exist_usr_info[nn] = np
with open('user_info.pickle','rb') as user_file:
pickle.dump(exist_usr_info,user_file)
tk.messagebox.showinfo('Welcome','you have successfully sign up!')
window_sign_up.destry()



#顶级窗口
window_sign_up = tk.Toplevel(window)
# window_sign_up.geometry('+400+400')
# window_sign_up.size(400,250)
window_sign_up.title("Sign ip window")

new_name = tk.StringVar()
#新用户名
new_name.set('chenfeihao')
tk.Label(window_sign_up,text = 'user name:').place(x=10,y=10)
entry_new_name = tk.Entry(window_sign_up,textvariable = new_name)
entry_new_name.place(x=80,y=10)

#新密码输入
new_pwd = tk.StringVar();
tk.Label(window_sign_up,text="password>").place(x=10,y=20)
#输入框
entry_new_pwd = tk.Entry(window_sign_up,textvariable = new_pwd,show='*')
entry_new_pwd.place(x=80,y=20)


new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up,text= 'Confirm passwd:').place(x=10,y=30)
entry_usr_pwd_confirm = tk.Entry(window_sign_up,textvariable=new_pwd_confirm)
entry_usr_pwd_confirm.place(x=80,y=30)

btn_confirm_sign_up = tk.Button(window_sign_up,text = 'Sign up',command = sign_to_Mofan_Python)
btn_confirm_sign_up.place(x=90,y=40)

btn_login = tk.Button(window,text = 'Log in',command=usr_login)
btn_login.place(x=270,y=230)
btn_sign_up = tk.Button(window,text = 'Sign up',command = usr_sign_up)
btn_sign_up.place(x=120,y=230)


window.mainloop()

'''
#导入需要的包
import tkinter as tk
from tkinter import messagebox # import this to fix messagebox error
import pickle
import webbrowser

window = tk.Tk()
window.title('Selen EngineView')
window.geometry('450x300')

# welcome image
#创建画布
canvas = tk.Canvas(window, height=200, width=500,bg='pink')
image_file = tk.PhotoImage(file='HQ.png')

#anchor 抛锚
image = canvas.create_image(180, 20, anchor='nw', image=image_file)
canvas.pack(side='top')

# user information
tk.Label(window, text='User name: ').place(x=120, y=150)
tk.Label(window, text='Password: ').place(x=120, y=190)

var_usr_name = tk.StringVar()
var_usr_name.set('selen')
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=200, y=150)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=200, y=190)


def usr_login():
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
try:
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usrs_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
webbrowser.open('http://www.baidu.com/')
# tk.messagebox.showinfo(title='Welcome', message='How are you? ' + usr_name)
else:
tk.messagebox.showerror(message='Error, your password is wrong, try again.')
else:
is_sign_up = tk.messagebox.askyesno('Welcome',
'You have not signed up yet. Sign up today?')
if is_sign_up:
usr_sign_up()


def usr_sign_up():
def sign_to_Mofan_Python():
np = new_pwd.get()
npf = new_pwd_confirm.get()
nn = new_name.get()
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
if np != npf:
tk.messagebox.showerror('Error', 'Password and confirm password must be the same!')
elif nn in exist_usr_info:
tk.messagebox.showerror('Error', 'The user has already signed up!')
else:
exist_usr_info[nn] = np
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('Welcome', 'You have successfully signed up!')
window_sign_up.destroy()

window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('Sign up window')

new_name = tk.StringVar()
new_name.set('chenfeihao')
tk.Label(window_sign_up, text='User name: ').place(x=10, y=10)
entry_new_name = tk.Entry(window_sign_up, textvariable=new_name)
entry_new_name.place(x=150, y=10)

new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='Password: ').place(x=10, y=50)
entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_usr_pwd.place(x=150, y=50)

new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y=90)
entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
entry_usr_pwd_confirm.place(x=150, y=90)

btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_Mofan_Python)
btn_comfirm_sign_up.place(x=150, y=130)


# login and sign up button
btn_login = tk.Button(window, text='Log in', command=usr_login)
btn_login.place(x=270, y=230)
btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=120, y=230)

window.mainloop()





























原文地址:https://www.cnblogs.com/countryboy666/p/11122249.html