通过上一节的操作,我们已经初步生了我们目标中定义的登录GUI代码了. 我们用notepad+打开刚才保存的py档:
通过PAGE生成python GUI界面(用PAGE拖出需要的GUI界面).py
以下这些内容可以全部delete掉.
import sys
import login_support
if __name__ == '__main__':
vp_start_gui()
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = Tk()
top = New_Toplevel (root)
login_support.init(root, top)
root.mainloop()
w = None
def create_New_Toplevel(root, *args, **kwargs):
'''Starting point when module is imported by another program.'''
global w, w_win, rt
rt = root
w = Toplevel (root)
top = New_Toplevel (w)
login_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_New_Toplevel():
global w
w.destroy()
w = None
以下这些内容也可以视情况精减,由于我们当是python3.x, 故库为tkinter, 也没有用到ttk, ttk的部分也可以del掉
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
import ttk
# py3 = False
except ImportError:
import tkinter.ttk as ttk
# py3 = True
故上面的代码只用保留以下这一句即可:
from tkinter import *
至此,现在只有class New_Toplevel这个部分了,去掉以下这部分,并将top.geometry()修改为top.geometry('AxB')的形式
# 以下内容直接删除
# _bgcolor = '#d9d9d9' # X11 color: 'gray85'
# _fgcolor = '#000000' # X11 color: 'black'
# _compcolor = '#d9d9d9' # X11 color: 'gray85'
# _ana1color = '#d9d9d9' # X11 color: 'gray85'
# _ana2color = '#d9d9d9' # X11 color: 'gray85'
font9 = "-family {Microsoft YaHei UI} -size 14 -weight normal " \
"-slant roman -underline 0 -overstrike 0"
# top.geometry("388x178+286+118") 修改为如下形式:
top.geometry("388x178")
最终于留下的可用的GUI代码如下:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import *
font9 = "-family {Microsoft YaHei UI} -size 14 -weight normal " \
"-slant roman -underline 0 -overstrike 0"
# 记得加上以下top的声明
top = Tk()
# top.geometry("388x178+286+118") 修改为如下形式:
top.geometry("388x178")
top.title("New Toplevel")
top.configure(background="#d9d9d9")
top.configure(highlightbackground="#d9d9d9")
top.configure(highlightcolor="black")
# 去掉Label(top)中的top
# Label1 = Label(top)
Label1 = Label()
Label1.place(relx=0.28, rely=0.02, height=31, width=57)
Label1.configure(activebackground="#f9f9f9")
Label1.configure(activeforeground="black")
Label1.configure(background="#d9d9d9")
Label1.configure(disabledforeground="#a3a3a3")
Label1.configure(font=font9)
Label1.configure(foreground="#000000")
Label1.configure(highlightbackground="#d9d9d9")
Label1.configure(highlightcolor="black")
Label1.configure(text='''Login''')
# Label2 = Label(top)
Label2 = Label()
Label2.place(relx=0.06, rely=0.34, height=23, width=71)
Label2.configure(activebackground="#f9f9f9")
Label2.configure(activeforeground="black")
Label2.configure(background="#d9d9d9")
Label2.configure(disabledforeground="#a3a3a3")
Label2.configure(foreground="#000000")
Label2.configure(highlightbackground="#d9d9d9")
Label2.configure(highlightcolor="black")
Label2.configure(justify=RIGHT)
Label2.configure(text='''UserName:''')
# Label3 = Label(top)
Label3 = Label()
Label3.place(relx=0.08, rely=0.56, height=23, width=65)
Label3.configure(activebackground="#f9f9f9")
Label3.configure(activeforeground="black")
Label3.configure(background="#d9d9d9")
Label3.configure(disabledforeground="#a3a3a3")
Label3.configure(foreground="#000000")
Label3.configure(highlightbackground="#d9d9d9")
Label3.configure(highlightcolor="black")
Label3.configure(justify=RIGHT)
Label3.configure(text='''Password:''')
# Entry1 = Entry(top)
Entry1 = Entry()
Entry1.place(relx=0.28, rely=0.34,height=27, relwidth=0.47)
Entry1.configure(background="white")
Entry1.configure(disabledforeground="#a3a3a3")
Entry1.configure(font="TkFixedFont")
Entry1.configure(foreground="#000000")
Entry1.configure(highlightbackground="#d9d9d9")
Entry1.configure(highlightcolor="black")
Entry1.configure(insertbackground="black")
Entry1.configure(selectbackground="#c4c4c4")
Entry1.configure(selectforeground="black")
# Entry2 = Entry(top)
Entry2 = Entry()
Entry2.place(relx=0.28, rely=0.56,height=27, relwidth=0.47)
Entry2.configure(background="white")
Entry2.configure(disabledforeground="#a3a3a3")
Entry2.configure(font="TkFixedFont")
Entry2.configure(foreground="#000000")
Entry2.configure(highlightbackground="#d9d9d9")
Entry2.configure(highlightcolor="black")
Entry2.configure(insertbackground="black")
Entry2.configure(selectbackground="#c4c4c4")
Entry2.configure(selectforeground="black")
# Button1 = Button(top)
Button1 = Button()
Button1.place(relx=0.7, rely=0.79, height=28, width=79)
Button1.configure(activebackground="#d9d9d9")
Button1.configure(activeforeground="#000000")
Button1.configure(background="#d9d9d9")
Button1.configure(disabledforeground="#a3a3a3")
Button1.configure(foreground="#000000")
Button1.configure(highlightbackground="#d9d9d9")
Button1.configure(highlightcolor="black")
Button1.configure(pady="0")
Button1.configure(text='''Confirm''')
# 记得加上这个
top.mainloop()
最终运行生成的界面如下:
至此,目标达到,别看写了这么多,其实非常简单,一点儿也不麻烦,如果你动动手,就明白真的很省事。