0

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 
Community
  • 1
  • 1
  • 3
    This doesn't answer your question, but you shouldn't assign a widget to a variable and `pack` it on the same line. Same for `grid`. See [this post](http://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-get) for more information. – Kevin Mar 31 '15 at 14:03

1 Answers1

2

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.

Alecg_O
  • 892
  • 1
  • 9
  • 19
  • That helps, however, can we center them? –  Mar 31 '15 at 14:11
  • Also, be sure to make note of Kevin's comment above - it would have saved me a lot of headaches in the past :P – Alecg_O Mar 31 '15 at 14:38
  • 1
    To center the buttons, put them inside another frame, and then pack that to the top. That frame will then be centered, and thus the buttons inside will be centered. – Bryan Oakley Mar 31 '15 at 15:15
  • I wasn't going to get into frames if you don't know how to use grid, but Bryan's comment is the more robust solution if you can implement it. – Alecg_O Mar 31 '15 at 15:19