Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

python - Align buttons and labels in each row with Tkinter

My code:

import tkinter

class latinwords:
    def __init__(self):
        self.main = tkinter.Tk()
        self.top = tkinter.Frame(self.main)
        self.mid = tkinter.Frame(self.main)
        
        self.latinword1 = tkinter.Button(self.mid, text = 'sinister', command = self.cbfunction)
        self.latinword2 = tkinter.Button(self.mid, text = 'dexter', command = self.cbfunction2)
        self.latinword3 = tkinter.Button(self.mid, text = 'medium', command = self.cbfunction3)
        
        self.toplabel = tkinter.Label(self.top, text= 'Latin')              
        self.toplabel2 = tkinter.Label(self.top, text= 'English')

        self.value = tkinter.StringVar()
        self.value1 = tkinter.StringVar()
        self.value2 = tkinter.StringVar()

        self.labels = tkinter.Label(self.bot, textvariable = self.value)
        self.labels1 = tkinter.Label(self.bot, textvariable = self.value1)
        self.labels2 = tkinter.Label(self.bot, textvariable = self.value2)
        self.labels.pack()
        self.labels1.pack()
        self.labels2.pack()
        
        
        self.top.pack()
        self.mid.pack()
        self.latinword1.pack()
        self.latinword2.pack()
        self.latinword3.pack()
        
        self.toplabel.pack(side='left')
        self.toplabel2.pack(side='left')
        tkinter.mainloop()
        
    def cbfunction(self):
        value = 'left'
        self.value1.set(value)
    def cbfunction2(self):
        value = 'right'
        self.value.set(value)
    def cbfunction3(self):
        value = 'centre'
        self.value2.set(value)
        


s = latinwords()

Unexpected output:

enter image description here

Expected output:

enter image description here

As you can see, I am trying to get my expected output with 3 buttons that can show the English word after its being pressed. But I got my output vertically with my own code. I am expecting my button and the matched word are on same horizontal level. Can anyone help me with this issue? Thanks.

question from:https://stackoverflow.com/questions/65914327/align-buttons-and-labels-in-each-row-with-tkinter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is better to put all the labels and buttons in same frame and use grid() instead of pack():

import tkinter

class latinwords:
    def __init__(self):
        self.main = tkinter.Tk()

        self.mid = tkinter.Frame(self.main)
        self.mid.pack()
        
        self.latinword1 = tkinter.Button(self.mid, text='sinister', command=self.cbfunction)
        self.latinword2 = tkinter.Button(self.mid, text='dexter', command=self.cbfunction2)
        self.latinword3 = tkinter.Button(self.mid, text='medium', command=self.cbfunction3)
        
        self.toplabel = tkinter.Label(self.mid, text='Latin')              
        self.toplabel2 = tkinter.Label(self.mid, text='English')

        self.value = tkinter.StringVar()
        self.value1 = tkinter.StringVar()
        self.value2 = tkinter.StringVar()

        self.labels = tkinter.Label(self.mid, textvariable=self.value)
        self.labels1 = tkinter.Label(self.mid, textvariable=self.value1)
        self.labels2 = tkinter.Label(self.mid, textvariable=self.value2)
        self.labels.grid(row=1, column=1)
        self.labels1.grid(row=2, column=1)
        self.labels2.grid(row=3, column=1)
        
        self.latinword1.grid(row=1, column=0)
        self.latinword2.grid(row=2, column=0)
        self.latinword3.grid(row=3, column=0)
        
        self.toplabel.grid(row=0, column=0)
        self.toplabel2.grid(row=0, column=1)

        tkinter.mainloop()
        
    def cbfunction(self):
        value = 'left'
        self.value.set(value)

    def cbfunction2(self):
        value = 'right'
        self.value1.set(value)

    def cbfunction3(self):
        value = 'centre'
        self.value2.set(value)

s = latinwords()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...