有没有办法使输出中心本身在tkinter中?

时间:2019-07-22 17:52:57

标签: python python-3.x tkinter tkinter-canvas

我想在矩形框中显示布局,当我们调整窗口大小(最大化/最小化)时,该矩形会居中。

import re
import tkinter as tk
root= tk.Tk()
root.title('Password Manager')
#root.iconbitmap("icon.ico")   # for icon in gui
root.geometry('500x500')
X=0
Y=0

tk.Label(root,text='User Name : ').place(x=X+0, y=Y+0, in_=root)
signin_user= tk.StringVar()    #getting username for signing in
e1=tk.Entry(root,textvariable=signin_user).place(x=X+70, y=Y+0, in_=root)   #User name for sign In

tk.Label(root,text='password : ').place(x=X+0, y=Y+25, in_=root)
signin_password=tk.StringVar()
e2=tk.Entry(root,textvariable=signin_password).place(x=X+70, y=Y+25, in_=root)      #Password for sign In

def sign_in():          #currently not working
    print('hello '+signin_user.get())
#sign In button
tk.Button(root,text="Sign In",width=10,command=sign_in).place(x=X+60,y=Y+50,in_=root)

tk.Label(root,text="Sign up", font='Helvetica 18 bold').place(x=X+50, y=Y+80, in_=root)


tk.Label(root,text="Name : ").place(x=X+0,y=Y+120,in_=root)           # Name for register
reg_name=tk.StringVar()
e3 = tk.Entry(root,textvariable=reg_name).place(x=X+50,y=Y+120,in_=root)

tk.Label(root,text="User Name : ").place(x=X+0,y=Y+145,in_=root)      #user name to register
reg_user=tk.StringVar()
e4 = tk.Entry(root,textvariable=reg_user).place(x=X+75,y=Y+145,in_=root)

tk.Label(root,text="Password : ").place(x=X+0,y=Y+170,in_=root)          #password to register
reg_password=tk.StringVar()
e5 = tk.Entry(root,textvariable=reg_password)
e5.place(x=X+65,y=Y+170,in_=root)

tk.Label(root,text="Confirm password : ").place(x=X+0,y=Y+195,in_=root)   #confirm password to register
reg_cnfpassword=tk.StringVar()
e6 = tk.Entry(root,textvariable=reg_cnfpassword).place(x=X+110,y=Y+195,in_=root)

    # []    A set of characters    "[a-m]"    
    # \    Signals a special sequence (can also be used to escape special characters)    "\d"    
    # .    Any character (except newline character)    "he..o"    
    # ^    Starts with    "^hello"    
    # $    Ends with    "world$"    
    # *    Zero or more occurrences    "aix*"    
    # +    One or more occurrences    "aix+"    
    # {}    Exactly the specified number of occurrences    "al{2}"    
    # |    Either or    "falls|stays"    
    # ()    Capture and group
m=tk.Message(root,text='',fg="red")
m.place(x=X+0,y=Y+250,in_=root)
def sign_up():
    regpass = "^[A-Z][\w(!@#$%^&*_+?)+]{8,}$"
    if not (re.search(regpass,reg_password.get())):
         m.configure(text='''->Spaces and empty sets are not allowed.
        \n ->First character should be a capital letter.
        \n ->Password must be greater than 8 character and must contain a special character.''')
    elif (reg_password != reg_cnfpassword):
        m.configure(text='Password and Confirm Password must match')
    else :
        m.configure(text='')




    #sign Up button
tk.Button(root,text="Sign Up",width=10,command=sign_up).place(x=X+80,y=Y+225,in_=root)

root.mainloop()

我尝试将其设计为框架,但未成功。 我也尝试过pack()解决,但再次失败了。 请同时提供使用任何方法获得所需输出的逻辑

1 个答案:

答案 0 :(得分:0)

我使用Frame将所有小部件放在grid()中,因此标签在第一列中,条目在第二列中。对于按钮,我使用columnspan,因此它们同时使用两列。

之后,我使用place(relx=0.5, rely=0.5, anchor='c')将窗口中的框架居中。调整窗口大小时,它保持居中。

我不需要变量XY来手动计算所有小部件的位置。

enter image description here

import re
import tkinter as tk

# --- functions ---

def sign_in():
    print('hello ' + signin_user.get())

def sign_up():
    # []    A set of characters    "[a-m]"    
    # \    Signals a special sequence (can also be used to escape special characters)    "\d"    
    # .    Any character (except newline character)    "he..o"    
    # ^    Starts with    "^hello"    
    # $    Ends with    "world$"    
    # *    Zero or more occurrences    "aix*"    
    # +    One or more occurrences    "aix+"    
    # {}    Exactly the specified number of occurrences    "al{2}"    
    # |    Either or    "falls|stays"    
    # ()    Capture and group
    regpass = "^[A-Z][\w(!@#$%^&*_+?)+]{8,}$"
    if not (re.search(regpass,reg_password.get())):
         m.configure(text='''->Spaces and empty sets are not allowed.
        \n ->First character should be a capital letter.
        \n ->Password must be greater than 8 character and must contain a special character.''')
    elif (reg_password != reg_cnfpassword):
        m.configure(text='Password and Confirm Password must match')
    else :
        m.configure(text='')

# --- main ---

root = tk.Tk()
root.title('Password Manager')
root.geometry('500x500')

signin_user = tk.StringVar()
signin_password = tk.StringVar()
reg_name = tk.StringVar()
reg_user = tk.StringVar()
reg_password = tk.StringVar()
reg_cnfpassword = tk.StringVar()


f = tk.Frame(root)
f.place(relx=0.5, rely=0.5, anchor='c')

tk.Label(f, text='User Name : ').grid(row=0, column=0, sticky='e')
tk.Entry(f, textvariable=signin_user).grid(row=0, column=1)

tk.Label(f, text='Password : ').grid(row=1, column=0, sticky='e')
tk.Entry(f, textvariable=signin_password).grid(row=1, column=1)

tk.Button(f, text="Sign In", width=10, command=sign_in).grid(row=2, column=0, columnspan=2)
tk.Label(f, text="Sign up", font='Helvetica 18 bold').grid(row=3, column=0, columnspan=2, pady=20)

tk.Label(f, text="Name : ").grid(row=4, column=0, sticky='e')
tk.Entry(f, textvariable=reg_name).grid(row=4, column=1)

tk.Label(f, text="User Name : ").grid(row=5, column=0, sticky='e')
tk.Entry(f, textvariable=reg_user).grid(row=5, column=1)

tk.Label(f, text="Password : ").grid(row=6, column=0, sticky='e')
tk.Entry(f, textvariable=reg_password).grid(row=6, column=1)

tk.Label(f, text="Confirm password : ").grid(row=7, column=0, sticky='e')
tk.Entry(f, textvariable=reg_cnfpassword).grid(row=7, column=1)

m = tk.Message(f, text='Hello World', bg="red")
m.grid(row=8, column=0, columnspan=2, sticky='we')

tk.Button(f, text="Sign Up", width=10, command=sign_up).grid(row=9, column=0, columnspan=2)

root.mainloop()