gpt4 book ai didi

python - 使用 Tkinter 对齐每行中的按钮和标签

转载 作者:行者123 更新时间:2023-12-05 06:06:02 24 4
gpt4 key购买 nike

我的代码:

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= '\tEnglish')

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()

意外输出:

enter image description here

预期输出:

enter image description here

如您所见,我试图通过 3 个按钮获得预期的输出,这些按钮在按下后可以显示英文单词。但是我用自己的代码垂直输出。我希望我的按钮和匹配的词在同一水平面上。谁能帮我解决这个问题?谢谢。

最佳答案

最好将所有标签和按钮放在同一个框架中,并使用 grid() 而不是 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()

关于python - 使用 Tkinter 对齐每行中的按钮和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65914327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com