user4433485
user4433485

Reputation:

Python Tkinter buttons

I am using multiple buttons in my code and they come among each other. I want them to be side by side, I have checked here: Setting the position on a button in Python? and have try'd the solution of that question,

However, I use .pack and I need to change it to grid? This is what I've got:

#import tKinter
import sys
from tkinter import *
def mhello():
    mlabel1 = Label(window,text = 'Hello we have to start / да започне').pack()
def mhello1():
    mlabel2 = Label(window,text = 'Hello we have to stop / да спре').pack()
def mhello2():
    mlabel3 = Label(window,text = 'Hello we have to add / да добавите').pack()    
#create new window
window = Tk()

#set window title
window.title("Изпитване програмата")
#set window size
window.geometry("700x200")

#menu start
def donothing():
   filewin = Toplevel(window)
   print("Welcome to my test program, XOXO Katherina") 
   mlabel5 = Label(window,text = 'Welcome to my test program, XOXO Katherina').pack()
def leave():
     sys.exit(0)

menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=window.destroy)
menubar.add_cascade(label="File", menu=filemenu)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

window.config(menu=menubar)
#menu ends 

#buttons here 
mlabel = Label(window,text = 'My label').pack()
mbutton = Button(window,text = 'Start',command = mhello, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
mbutton1 = Button(window,text = 'Stop',command = mhello1, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
mbutton2 = Button(window,text = 'Add',command = mhello2, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
#end buttons
#draw the window start application
window.mainloop()

also have try'd to change the buttons to:

#buttons here 
mlabel = Label(window,text = 'My label').pack()
mbutton = Button(window,text = 'Start',command = mhello, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton.grid(row=0, column=0)

mbutton1 = Button(window,text = 'Stop',command = mhello1, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton1.grid(row=1, column=0)

mbutton2 = Button(window,text = 'Add',command = mhello2, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton2.grid(row=2, column=0)
#end buttons 

Upvotes: 0

Views: 383

Answers (1)

Alecg_O
Alecg_O

Reputation: 1041

Use pack(side=LEFT) or pack(side=RIGHT) to place things side by side using pack.

In general however, if you are doing anything beyond this project it is definitely a good idea to learn to use grid(). It's a little more complicated but provides much more flexibility for placement without much work. A good reference on how to use each and the differences between them can be found at this site.

Update

Getting things centered on the same line is a little more tricky if you are still stuck on pack(). There is no direct way to make it work, the easiest workaround is to create something to fill the space on the side so the buttons are forced to the middle. Something like this should do the trick:

spacing1 = Label(root, text=" "*50).pack(side=LEFT)
button1 = Button(...)
button1.pack(side=Left)
spacing2 = Label(root, text=" "*50).pack(side=RIGHT)
button2 = Button(...)
button2.pack(side=RIGHT)

Note that you will have to figure out the correct number of spaces to make the buttons appear centered through trial and error.

Upvotes: 2

Related Questions