Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.I have a problem with tkinter in python3.I want to put "Confirm" button next to Entry.How can I do it?
photo[^]

Thanks.

What I have tried:

from tkinter import *
import tkinter.font as tkFont
root = Tk()

def Password(root , unlock_message , access_camera):    

    activate_message = "SYSTEM ACTIVATED"
    denied_message = "ACCESS DENIED"

    size_letter = tkFont.Font(family = "Helvetica" , size = 24 , weight = "bold")
    message_box = Label(root , text = "Passcode required to " + unlock_message + " system" , padx = 500 , font = size_letter)
    message_box.grid(row = 0 , column = 0)

    input_box = Entry(root , show = "*" , width = 6 , font = ('Verdana' , 20) ).grid(row = 1 , column = 0)
    enter_password_button = Button(root , text = "Confirm"  , pady = 8).grid(row = 1 , column = 1)

    
root.geometry("1800x1100")
root.title("Vehicle app")
Password(root , "unlock" , True)

root.mainloop()
Posted
Updated 6-Mar-21 8:15am
v2
Comments
Richard MacCutchan 30-Jan-21 4:46am    
Change your padding values. You really need to spend some time studying the Tk libraries and how to mage widgets on screen.

As I understand it, this is what you want:
                     column 0                          column 1
      +-------------------------------------+--------------------------------+
      |                                     |                                | 
row 0 |                      Passcode required to unlock system              | 
      |                                     |                                | 
      +-------------------------------------+--------------------------------+
row 1 |                        entry widget | Button widget                  |
      +-------------------------------------+--------------------------------+
When using the grid manager, it really helps making a sketch with pencil and paper of the layout you want to achieve.
I like to put one widget per grid cell and then play with span, padding and row and column configuration (weight dictates if row/column will stretch and if so, their relative dimensions).

Here is your code modified for the layout shown above (read the comments):
from tkinter import *
import tkinter.font as tkFont
root = Tk()

def Password(root , unlock_message , access_camera):

    activate_message = "SYSTEM ACTIVATED"
    denied_message = "ACCESS DENIED"

    size_letter = tkFont.Font(family = "Helvetica" , size = 24 , weight = "bold")
    
    message_box = Label(root , text = "Passcode required to " + unlock_message + " system" , padx = 500 , font = size_letter)
    # the label goes in row 0, column 0 and spans for 2 columns (right above the entry and the button)
    # no sticky option spceified means it will be centered
    message_box.grid(row = 0 , column = 0, columnspan=2)

    # the entry widget goes in row 1 column 0
    # it should stick to the right hand side (East). A horizontal padding of 5 provides some separation
    input_box = Entry(root , show = "*" , width = 6 , font = ('Verdana' , 20) ).grid(row = 1 , column = 0,  padx = 5, sticky='e')

    # the button widget goes in row 1 column 1
    # it should stick to the left hand side (West). A horizontal padding of 5 provides some separation
    enter_password_button = Button(root , text = "Confirm"  , pady = 8)
    enter_password_button.grid(row = 1 , column = 1, padx = 5, sticky='w')

    root.columnconfigure(0, weight=1) # columnn 0 should stretch -> weight=1
    root.columnconfigure(1, weight=1) # columnn 1 should stretch -> weight=1
    #root.rowconfigure(0, weight=0)    # row 0 should not stretch -> weight=0 (this is the default, so this line is not actually needed)

root.geometry("1800x1100")
root.title("Vehicle app")
Password(root , "unlock" , True)

root.mainloop()

Note a potential error in your code, in line
enter_password_button = Button(root , text = "Confirm"  , pady = 8).grid(row = 1 , column = 1)

You're actually assigning None to enter_password_button, because that's what .grid() returns.
If you want to use enter_password_button later, you have to split the single line into two lines:
enter_password_button = Button(root , text = "Confirm"  , pady = 8)
enter_password_button.grid(row = 1 , column = 1, padx = 5, sticky='w')
 
Share this answer
 
v2
You can adjust the confirm button with place(), like this:
from tkinter import *
import tkinter.font as tkFont
root = Tk()

def Password(root , unlock_message , access_camera):    

    activate_message = "SYSTEM ACTIVATED"
    denied_message = "ACCESS DENIED"

    size_letter = tkFont.Font(family = "Helvetica" , size = 24 , weight = "bold")
    message_box = Label(root , text = "Passcode required to " + unlock_message + " system" , padx = 500 , font = size_letter)
    message_box.grid(row = 0 , column = 0)

    input_box = Entry(root , show = "*" , width = 6 , font = ('Verdana' , 20) ).grid(row = 1 , column = 0)
    enter_password_button = Button(root , text = "Confirm"  , pady = 8).place(x=850, y=40)

    
root.geometry("1800x1100")
root.title("Vehicle app")
Password(root , "unlock" , True)

root.mainloop()

for eg. here I have set the value of x to 850 and y to 40 to align the confirm button
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900