Python Forum
the alternate keyboard commands / center command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
the alternate keyboard commands / center command
#1
Good news – I've been working through tkinter tutorials and am learning.  The following code runs, at least it creates a GUI.  

from tkinter import *

root = Tk()

root.title("Credentials")

root.geometry("260x94")  # specify size of window
root.resizable(0,0)   # make window non-resizable

### Set up frames
primaryFrame = Frame(root)
primaryFrame.pack()

secondaryFrame = Frame(root)
secondaryFrame.pack(side=BOTTOM)

##Set up Labels
labelName = Label(primaryFrame, text="Name")
labelPassword = Label(primaryFrame, text="Password")

## Create entry fields
entry_Name = Entry(primaryFrame)
entry_Password = Entry(primaryFrame)

## Put labels where they belong on grid
labelName.grid(row=0, sticky=E)
labelPassword.grid(row=1, sticky=E)

## Put entry fields where they belong
entry_Name.grid(row=0, column=1)
entry_Password.grid(row=1, column=1)

btnOK = Button(secondaryFrame, text="OK")   # create OK button
btnOK.grid(row=0, column=0)                 # place OK button on form

btnCancel = Button(secondaryFrame, text="Cancel")   # create Cancel button
btnCancel.grid(row=0, column=1)                     # place cancel button on form

chkLoggedIn = Checkbutton(secondaryFrame, text="Keep me logged in")
#chkLoggedIn.pack()
chkLoggedIn.grid(row=1, columnspan=2)

root.mainloop()
I'm glad that this code runs, but I do have some questions.  
1. I have an OK button and a Cancel button.  You can lick on them with the mouse.  Good so far.  However, I want the user to have the option of using his keyboard with Alt+O for OK or Alt+C for cancel.  In VB, I would have put an ampersand in front of the “O” in “OK” and then “O” would have been underlined and the user could press Alt+O in lieu of using the mouse.  However, in tkinter that only puts an ampersand in front of that word. Keyboard commands are important because some people prefer them; plus, sometimes mice quit working.  How do I put in the keyboard commands?
2. The GUI comes up at the top of my screen.  I had hoped that there would be a center command, as in “root.center,” but that appears not to exist, or at least my PyCharm IDE does not offer it.  How could I center this form?  

By the way, I know I could have created this form without using two frames.  It could have been created with just one frame or with none at all.  I used two frames just for the purpose of learning how to do so.
Reply
#2
Here's a list of key names: http://infohost.nmt.edu/tcc/help/pubs/tk...names.html
The same reference covers how to use them with bind statements.
Reply
#3
(Apr-29-2017, 08:54 PM)Larz60+ Wrote: Here's a list of key names: http://infohost.nmt.edu/tcc/help/pubs/tk...names.html
The same reference covers how to use them with bind statements.

Thanks a bunch for your help.
Reply
#4
You can use the proper geometry string to position the GUI in the center of the screen.
width, height = (260, 94) # width and height of your form
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk root window
x = (ws - width) / 2 
y = (hs - height) / 2
window_centered_geometry = '{:d}x{:d}+{:d}+{:d}'.format(width, height, x, y*2/3)
root.geometry(window_centered_geometry)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGTK] How to center text on multi-line buttons? Lomax 3 4,225 Jan-23-2021, 03:23 PM
Last Post: Lomax
  Spacing Between two labels are very far and Top label is not in Center using Tkinter barry76 2 7,060 Jul-30-2019, 10:49 AM
Last Post: wuf
  [Tkinter] How do I center this text in tkinter? ejected 4 63,527 Mar-29-2019, 10:35 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020