Apr-29-2017, 08:07 PM
Good news – I've been working through tkinter tutorials and am learning. The following code runs, at least it creates a GUI.
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.
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.
