I have been trying to create a small GUI for a definition-tester program I am making. My GUI needs to look like this:
Word: # label, then entry widget
Definition: # label, entry widget
Part of Speech: # label, then entry widget
Go Quit # each are buttons
This is what I have so far:
from Tkinter import *
class GetWord:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()
self.wordL = Label(self.frame, text = 'Word: ')
self.wordL.pack(side = 'left')
self.wordE = Entry(self.frame)
self.wordE.pack(side = 'right', after=self.wordL)
self.defL = Label(self.frame, text = 'Definitions: ')
self.defL.pack(side = 'left', after=self.wordE)
self.defE = Entry(self.frame)
self.defE.pack(side = 'right', after=self.defL)
self.posL = Label(self.frame, text = 'Part of Speech: ')
self.posL.pack(side = 'left', after=self.defE)
self.posE = Entry(self.frame)
self.posE.pack(side = 'right', after=self.posL)
self.buttonE = Button(self.frame, text = 'Go', command = self.getInfo)
self.buttonE.pack(side='bottom', after=self.posE)
self.quitB = Button(self.frame, text = 'Quit', fg = 'red', command = self.frame.quit)
self.quitB.pack(side='bottom', after=self.buttonE)
def getInfo(self):
print self.wordE.get(), self.defE.get(), self.posE.get()
root = Tk()
f = GetWord(root)
root.mainloop()
However, they don’t line up. How can I associate them each as a ‘row’ (one label, one entry per ‘row’, and a final ‘row’ with the two buttons)/
Also, I’m not sure how this part works:
root = Tk()
f = GetWord(root)
root.mainloop()
root does not directly reference the GetWord class, so I don’t know how it is working. If someone could explain this to me, that’d be great. Thanks!
To do this, you must substitute
packwithgrid. When usinggrid, you must first name the widget (for example)f = Label(#info). Then, you set thegrid, which is similar to pack, but allows more control.f.grid(row =1, column = 1, sticky = W). To understand more aboutgrid, look here: http://effbot.org/tkinterbook/grid.htm