Problems with tkinter grid layout in python - python

I'm doing the Angela Yu 100 days of code python course and im having trouble with the grid layout in tkinter package. I don't understand why my Entry objects aren't directly under each other. I used the same padding and size values as in the course and i tried to change them to look better but the password entry and the add button are more on the left then other entries while they have the same column number in the grid function. Thank u for ur help ^^
Here is the code i used for interface:
window = Tk()
window.title("Password Manager")
window.config(padx=50, pady=50)
logo = PhotoImage(file="logo.png")
image = Canvas(width=200, height=200, highlightthicknes=0)
image.create_image(100, 100, image=logo)
image.grid(column=1, row=0)
website = Label(text="Website:")
website.grid(column=0, row=1)
email = Label(text="Email/Username:")
email.grid(column=0, row=2)
password = Label(text="Password:")
password.grid(column=0, row=3)
website_blank = Entry(width=36)
website_blank.grid(column=1, row=1, columnspan=2)
website_blank.focus()
email_blank = Entry(width=36)
email_blank.grid(column=1, row=2, columnspan=2)
email_blank.insert(0, "aleksander.jaloszynski#gmail.com")
password_blank = Entry(width=21)
password_blank.grid(column=1, row=3)
generate_password = Button(text="Generate Password")
generate_password.grid(column=2, row=3)
add = Button(text="Add", width=36, command=save)
add.grid(column=1, row=4, columnspan=2)
window.mainloop()
And here is how it looks:
enter image description here

Related

Putting two buttons next to each other with Tkinter

I was coding a GUI with the Tkinter library in Python and I've ran into the following problem:
# DECLARATIONS
labelPresentation = Label(window, text="Insert here")
name = StringVar()
nameEntered = Entry(window, width=25, textvariable=name)
labelPresent = Label(window)
research = ttk.Button(window, text="Search", command=searchToPut)
elimination = ttk.Button(window, text="Delete", command=searchToDelete)
# POSITIONS
labelPresentation.grid(column=0, row=0)
nameEntered.grid(column=0, row=1)
labelPresent.grid(column=0, row=3)
research.grid(column=0, row=2)
elimination.grid(column=0, row=2)
I need to put the elimination and research button next to each other but the grid function is causing me problems, because this code put them one in top of the other. I searched for the same problem and they suggested to use the pack() function, but when I try it it says that I can't use both grid and pack function to place buttons and labels.
Thanks in advance for the help!
Try this:
# DECLARATIONS
labelPresentation = Label(window, text="Insert here")
name = StringVar()
nameEntered = Entry(window, width=25, textvariable=name)
labelPresent = Label(window)
research = ttk.Button(window, text="Search", command = searchToPut)
elimination = ttk.Button(window, text="Delete", command =searchToDelete )
# POSITIONS
#labelPresentation.grid( row=0)
labelPresentation.place(x=50)
nameEntered.place(x=0,y=20)
labelPresent.grid( row=0)
research.grid(row=2,column=0)
elimination.grid(row=2, column = 1 , padx=0,pady = 25)
Output:

Button on far right despite only being in 2nd (1) column

import random
from tkinter import *
root = Tk()
FirstPageFrame = Frame(root) # CREATES FIRST PAGE FRAME
FirstPageFrame.pack() # CREATES FIRST PAGE FRAME
RolltheDiceTitle = Label(FirstPageFrame, text="Roll the Dice")
RolltheDiceTitle.config(font=("Courier", 30))
RolltheDiceTitle.grid()
LoginRegisterWelcomeMessage = Label(FirstPageFrame, text="Welcome to the Roll the Dice Game.")
LoginRegisterWelcomeMessage.grid(row=1, padx=10, pady=10)
DiceImage = PhotoImage(file="dice.png")
DiceImageLabel = Label(FirstPageFrame, image=DiceImage)
DiceImageLabel.grid()
registerButton = Button(FirstPageFrame, text="Register", fg="orange") # CREATES REGISTER BUTTON
registerButton.grid(row=4, padx=10, pady=10)
loginButton = Button(FirstPageFrame, text="Login", fg="Green") # CREATES LOGIN BUTTON
loginButton.grid(row=4, column=1, padx=10, pady=10)
root.mainloop() # Continues tkinter window loop so program does not close
I'm trying to have both the register and login buttons in the middle of the window (where register button is located) but side by side, not stacked.
Apologies for any incorrect formatting with this question or any easy fixes with the code. I'm super new to Tkinter and trying to figure stuff out :)
When you put the Login button in column 1 all the other widgets are in column 0, so the Login button will be displayed on the far right.
One solution is to create a contaner frame for the buttons which can be placed in the center of column 0, and then place the buttons in culmns in that frame.
I have stated row and colun explicitly for all widgets which makes it easier to follow, and also assigned a bg color to the buttonFrame so you can see where it goes.
import random
from tkinter import *
root = Tk()
FirstPageFrame = Frame(root) # CREATES FIRST PAGE FRAME
FirstPageFrame.pack() # CREATES FIRST PAGE FRAME
RolltheDiceTitle = Label(FirstPageFrame, text="Roll the Dice")
RolltheDiceTitle.config(font=("Courier", 30))
RolltheDiceTitle.grid(row=0, column=0)
LoginRegisterWelcomeMessage = Label(FirstPageFrame, text="Welcome to the Roll the Dice Game.")
LoginRegisterWelcomeMessage.grid(row=1, column=0, padx=10, pady=10)
DiceImage = PhotoImage(file="dice.png")
DiceImageLabel = Label(FirstPageFrame, image=DiceImage)
DiceImageLabel.grid(row=2, column=0)
# Here is the container frame for the buttons.
buttonFrame = Frame(FirstPageFrame, bg='tan') # bg color to indicate
buttonFrame.grid(row=3) # position and extent of frame
registerButton = Button(buttonFrame, text="Register", fg="orange") # CREATES REGISTER BUTTON
registerButton.grid(row=0, column=0, padx=10, pady=10)
loginButton = Button(buttonFrame, text="Login", fg="Green") # CREATES LOGIN BUTTON
loginButton.grid(row=0, column=1, padx=10, pady=10)
root.mainloop() # Continues tkinter window loop so program does not close

How to create a drop down list using tkinter and set it in a specific row/column? (Python)

I'm having some difficulties using tkinter to create a drop down list.
I'm not looking for creating a menu i want to make a little app to let me add new entries to an excel file.
Some of the entries are directly typed by the user but others must be selected from a drop down list.
I don't know how to set up this lists so that they appear at the right row/ column.
here is my code:
import tkinter as tk
window = tk.Tk()
window.title("Ajouter une nouvelle entrée")
window.geometry('400x350')
lbl1 = tk.Label(window, text="Numéro OF")
e1=tk.Entry()
lbl1.grid(column=0, row=0)
e1.grid(column=1,row=0)
lbl2 = tk.Label(window, text="Produit/carte")
e2=tk.Entry()
e2.grid(column=1,row=1)
lbl2.grid(column=0, row=1)
lbl3 = tk.Label(window, text="Famille")
lbl3.grid(column=0, row=2)
lbl4 = tk.Label(window, text="Test")
lbl4.grid(column=0, row=3)
lbl5 = tk.Label(window, text="Constats technique")
e5= tk.Entry()
e5.grid(column=1,row=4)
lbl5.grid(column=0, row=4)
lbl6 = tk.Label(window, text="Type de panne")
lbl6.grid(column=0, row=5)
lbl7 = tk.Label(window, text="Solutions")
e7= tk.Entry()
e7.grid(column=1,row=6)
lbl7.grid(column=0, row=6)
lbl8 = tk.Label(window, text="Composant changé")
e8= tk.Entry()
e8.grid(column=1,row=7)
lbl8.grid(column=0, row=7)
lbl9 = tk.Label(window, text="Qui?")
lbl9.grid(column=0, row=8)
lbl10 = tk.Label(window, text="Quand")
e10= tk.Entry()
e10.grid(column=1,row=9)
lbl10.grid(column=0, row=9)
lbl11 = tk.Label(window, text="Etat")
lbl11.grid(column=0, row=10)
lbl12 = tk.Label(window, text="Commentaires")
e12=tk.Entry()
e12.grid(column=1,row=11)
lbl12.grid(column=0, row=11)
window.mainloop()
you can see that the lables that have an entry to their right side don't need to be modified but for lbl3, lbl4, lbl6,lbl9, and lbl11 i need drop down lists.
for example for lbl3 i need a list in column 1 row 2 with this 4 entries:
'XS2444', 'XB3248', 'DEf23', DEf24' and 'DEf2500'.
You will see it more clearly when you open the window.
Moreover for lbl6 i have a list of more than 100 items to put in the drop down list, I don't know how to add them automatically so that i don't have to enter every item.
Thank you very much you will be truly helping!

Taking in a user input to load a program

Within a revision application I'm developing, I'm looking to take in a user answer (of which area of revision they would like to focus upon) which will then load the specific area they need.
Would this require entry widgets?
This is the code I have so far:
instruction = tkinter.Label(roots, text= "'What would you like to revise?'\n")
instruction.grid(row=0, column=0,sticky=W)
if int(answer) != int(entryWidget.get().strip()):
tkMessageBox.showinfo("Answer", "INCORRECT!")
else:
tkMessageBox.showinfo("Answer", "CORRECT!")
I apologize for the lack of code, but I'm unsure on how to develop it past this point without knowing how to load my alternate part of code from this point
part 2:
def Signup():
global pwordE
global nameE
global roots
roots = tkinter.Tk()
roots.title("Computer Science Revision")
roots.geometry("1000x1000")
roots.wm_iconbitmap('favicon.ico')
roots.configure(background="#a1dbcd")
photo= tkinter.PhotoImage(roots,file="ryu.gif")
A = tkinter.Label(roots,image=photo)
A.pack()
roots = tkinter.Tk()
roots.title('Signup')
instruction = tkinter.Label(roots, text= 'Please enter new Credentials\n')
instruction.grid(row=0, column=0,sticky=W)
nameL = tkinter.Label(roots, text='New Username: ')
pwordL = tkinter.Label(roots, text='New Password: ')
nameL.grid(row=1, column=0, sticky=W)
pwordL.grid(row=2, column=0, sticky=W)
nameE= tkinter.Entry(roots)
pwordE = tkinter.Entry(roots, show='*')
nameE.grid(row=1, column=1)
pwordE.grid(row=2, column=1)
signupButton = Button(roots, text='Signup', command=FSSignup)
signupButton.grid(columnspan=2, sticky=W)
roots.mainloop()
roots.title("Computer Science Revision")
roots.geometry("1000x1000")
roots.wm_iconbitmap('favicon.ico')
roots.configure(background="#a1dbcd")
photo= tkinter.PhotoImage(roots,file="ryu.gif")
A = tkinter.Label(roots,image=photo)
A.pack()
This part will not load with the rest of the login, how can I fix this?
I Hope you are asking something like this one.
import tkinter as tk
root = tk.Tk()
root.geometry("500x500+100+100")
def raise_frame(frame):
frame.tkraise()
f1 = tk.Frame(root)
f2 = tk.Frame(root)
f3 = tk.Frame(root)
f4 = tk.Frame(root)
for frame in (f1,f2,f3,f4):
frame.grid(row=0, column=0, sticky='news')
#Frame1
button1 = tk.Button(f1, text='English', command=lambda:raise_frame(f2)).pack()
button2 = tk.Button(f1, text='Maths', command=lambda:raise_frame(f3)).pack()
button3 = tk.Button(f1, text='Science', command=lambda:raise_frame(f4)).pack()
#Frame2
tk.Label(f2, text="English Revision").pack()
tk.Button(f2, text="HOME", command=lambda:raise_frame(f1)).pack()
#Frame3
tk.Label(f3, text="Maths Revision").pack()
tk.Button(f3, text="HOME", command=lambda:raise_frame(f1)).pack()
#Frame4
tk.Label(f4, text="Science Revision").pack()
tk.Button(f4, text="HOME", command=lambda:raise_frame(f1)).pack()
raise_frame(f1)
root.mainloop()

scroll bar tkinter not scrolling

#declare gui object and classes
app = Tk() #creates instance of Tk()
app.title('Check sort DCA') # sets title of gui
#---------------------------------------
def keepSuggested(): #button press actions
es.JournalOut('test2')
def UseNew():
es.JournalOut('test1')
#------------------------------
frame=Frame(app,width=500,height=500)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,500))
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=500,height=500)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(expand=True,fill=BOTH)
spacer1 = Label(canvas, text='|')
spacer2 = Label(canvas, text='|')
spacer3 = Label(canvas, text='|')
spacer4 = Label(canvas, text='|')
spacer5 = Label(canvas, text='|')
Chan_Num = Label(canvas,text='Channel Number')
Chan_Name = Label(canvas, text='Channel Name')
NewChan_Num = Label(canvas, text='New Channel Number')
Set_Title = Label(canvas, text='Set New')
std_Num=Label(canvas, text='Standard Channel Number')
std_Name = Label(canvas, text='Standard Channel Name')
Chan_Num.grid(row=0, column=0)
spacer1.grid(row=0, column=1)
Chan_Name.grid(row=0, column=2)
spacer2.grid(row=0, column=3)
NewChan_Num.grid(row=0, column=4)
spacer3.grid(row=0, column=5)
Set_Title.grid(row=0, column=6)
spacer4.grid(row=0,column=7)
std_Num.grid(row=0,column=8)
spacer5.grid(row=0,column=9)
std_Name.grid(row=0,column=10)
n=0
i = 0 # loops through all channel numbers to get print table value.
while i < nchan: # prints out all present channels with index and channel number and title #populates tables
ch_name = tsin.GetChanTitle(i)
ch_num = tsin.GetChanNumber(i)
ch_name_list = Label(canvas, text=ch_name )
ch_num_list = Label(canvas, text=str(ch_num))
ch_name_list.grid(row=i + 1, column=2)
ch_num_list.grid(row=i + 1, column=0)
UserInput=StringVar()
EntryBox= Entry(canvas, textvariable = UserInput)
EntryBox.grid(row=i+1,column=4 )
i = i + 1
j=0
while j< len(CorrectChannels):
stdList= CorrectChannels[j]
stdListNum = j
std_ch_num= Label(canvas,text=stdListNum+1)
std_ch_name = Label(canvas,text=stdList)
std_ch_name.grid(row=j+1, column=10)
std_ch_num.grid(row=j+1, column=8)
j=j+1
#build gui elements
Buttonnew = Button(canvas, text='Set Channels', bg='blue', fg='white',command=UseNew)
Buttonnew.grid(row=1, column=6)
Buttonkeep = Button(canvas, text='keep channels', bg='blue', fg='white', command=keepSuggested)
Buttonkeep.grid(row=2, column=6)
app.mainloop()
When I run my tkinter code python code I get a scroll bar with no scroll ability, all of my widgets are in canvas and display properly however scroll is needed to scroll down to see them all, this code is producing a scroll bar however it isn't working.
Your scrollbar works fine. However, you've defined the scrollregion exactly the same as the size of the canvas. Therefore there is nothing to scroll to. Try
canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,800))
And you will see that you can scroll down 300 pixels.
Full working example code:
app = Tk()
frame=Frame(app,width=500,height=500)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,800))
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(yscrollcommand=vbar.set)
canvas.pack()
canvas.create_rectangle((200,300,300,600))
app.mainloop()
From your comment, I get the impression that you are using grid to place widgets on your Canvas. You should not do that, a Canvas is not a Frame in which you can grid widgets. You can create shapes on a Canvas or create a window that contains widgets. If you are trying to make a grid of widgets scrollable, you should place the Frame on the Canvas, not the other way around. See this answer for a great example of making a grid of widgets scrollable.

Categories

Resources