首页 > 解决方案 > Tkinter多行文字定位问题

问题描述

我在 Python 上的 tkinter 程序有问题。在第 11 行\n,由于所有文本都不适合我设置的窗口大小,因此我已经放置了一个。我希望新行从右侧开始(例如在文本编辑器上按 Enter)。当一个新行被调用时它会出现,它以中心为基础的文本。我是 tkitner 的新手,因此不了解所有命令/功能,因此将不胜感激。我已经添加了问题的照片。


def password_basic_check():#Function that gets called when enter button is pressed. function check password matches. its lengh and if it has both upper and lower case letters
    if password_enter_txt.get() != password_reenter_txt.get():#checks if both passwords matches
        password_fault_report.configure(text='Passwords do not match, try again.')#prints this text out into the blank string

    elif len(password_reenter_txt.get()) < 8:#checks if password lengh is below 8 characters
        password_fault_report.configure(text='Password needs to be at least 8 characters long')

    elif password_reenter_txt.get().islower() == True or password_reenter_txt.get().isupper() == True:#if password returns true if string has lower and upper case letters are used
        password_fault_report.configure(text='Password needs have both upper\n and lower case lettes')

    else:
        password_fault_report.configure(text='Password correct')#if all of the password checks fail

window = Tk()
window.geometry('480x320') #setting size of window (pixles)
window.title('Password Checker')
window.iconbitmap('/Users/andypaling/Desktop/index.ico')#sets icon for window

welcome_lbl = Label(window, text='Welcome', font=('Arial', 26), fg='#71C2FE') #Label with text saying 'welcome' in light blue 
welcome_lbl.place(x=15, y=10) #placing welcome label in top left of window

under_line_lbl = Label(window, text='__________________', font=('Arial', 35), fg='#FFB56B') #orange line under blue for looks
under_line_lbl.place(x=0, y=40)

intro_lbl = Label(window, text='Please enter your password: ', font=('Arial', 14, 'bold italic'))#lable with text underlined and italics
intro_lbl.place(x=10, y=90)

password_enter_lbl = Label(window, text='Password: ', font=('Candara', 10))
password_enter_lbl.place(x=10, y=120)

password_enter_txt = Entry(window, width=20, relief='raised')#entry box for user to enter their password
password_enter_txt.place(x=10, y=135)

password_reenter_lbl = Label(window, text='Re-enter password', font=('Candara', 10))#same as above for password reenter lbl and entry
password_reenter_lbl.place(x=10, y=160)

password_reenter_txt = Entry(window, width=20, relief='raised')
password_reenter_txt.place(x=10, y=175)

password_fault_report = Label(window, text='', font=('Arial', 14))#Empty label to be filled later to report password errors 
password_fault_report.place(x=225, y=140)

password_enter_btn = Button(window, text='Enter.', relief='raised', width=6, bg='orange', command=password_basic_check)#button to enter password, when pressed, password_basic_check is called
password_enter_btn.place(x=10, y=210)


window.mainloop()

[photo of problem][1]


  [1]: https://i.stack.imgur.com/GdQ1Y.png

标签: pythontkinter

解决方案


如果您在这里没有问题,您甚至可以选择两个标签这是最简单的解决方案

from tkinter import *


def password_basic_check():  # Function that gets called when enter button is pressed. function check password matches. its lengh and if it has both upper and lower case letters
    password_fault_report1.configure(text='')
    password_fault_report2.configure(text='')
    if password_enter_txt.get() != password_reenter_txt.get():  # checks if both passwords matches
        password_fault_report1.configure(
            text='Passwords do not match, try again.')  # prints this text out into the blank string

    elif len(password_reenter_txt.get()) < 8:  # checks if password lengh is below 8 characters
        password_fault_report1.configure(text='Password needs to be at least')
        password_fault_report2.configure(text='8 characters long')

    elif password_reenter_txt.get().islower() == True or password_reenter_txt.get().isupper() == True:  # if password returns true if string has lower and upper case letters are used
        password_fault_report1.configure(text='Password needs have both upper')
        password_fault_report2.configure(text='and lower case lettes')

    else:
        password_fault_report1.configure(text='Password correct')  # if all of the password checks fail


window = Tk()
window.geometry('480x320')  # setting size of window (pixles)
window.title('Password Checker')

welcome_lbl = Label(window, text='Welcome', font=('Arial', 26),
                    fg='#71C2FE')  # Label with text saying 'welcome' in light blue
welcome_lbl.place(x=15, y=10)  # placing welcome label in top left of window

under_line_lbl = Label(window, text='__________________', font=('Arial', 35),
                       fg='#FFB56B')  # orange line under blue for looks
under_line_lbl.place(x=0, y=40)

intro_lbl = Label(window, text='Please enter your password: ',
                  font=('Arial', 14, 'bold italic'))  # lable with text underlined and italics
intro_lbl.place(x=10, y=90)

password_enter_lbl = Label(window, text='Password: ', font=('Candara', 10))
password_enter_lbl.place(x=10, y=120)

password_enter_txt = Entry(window, width=20, relief='raised')  # entry box for user to enter their password
password_enter_txt.place(x=10, y=135)

password_reenter_lbl = Label(window, text='Re-enter password',
                             font=('Candara', 10))  # same as above for password reenter lbl and entry
password_reenter_lbl.place(x=10, y=160)

password_reenter_txt = Entry(window, width=20, relief='raised')
password_reenter_txt.place(x=10, y=175)

password_fault_report1 = Label(window, text='',
                               font=('Arial', 14))  # Empty label to be filled later to report password errors
password_fault_report1.place(x=150, y=140)
password_fault_report2 = Label(window, text='',
                               font=('Arial', 14))  # Empty label to be filled later to report password errors
password_fault_report2.place(x=150, y=170)


password_enter_btn = Button(window, text='Enter.', relief='raised', width=6, bg='orange',
                            command=password_basic_check)  # button to enter password, when pressed, password_basic_check is called
password_enter_btn.place(x=10, y=210)

window.mainloop()

这可能会有所帮助:)


推荐阅读