无法在tkinter中将我的按钮放置到指定的一侧

时间:2018-07-26 05:36:21

标签: python tkinter python-3.6

我在python 3.6中使用了tkinter,因此我可以学习GUI编程。我试图创建一个按钮。当我尝试packleftrighttopbottom的按钮时,没有得到输出,而side=top适用于框架命令。请帮助我。预先感谢。

         from tkinter import *
         import tkinter as tk
         root = tk.Tk()
         topframe = Frame(root)
         topframe.pack()
         bottomframe = Frame(root)
         bottomframe.pack(side=BOTTOM)
         button1 = Button(topframe, text="helo boys", fg="green")
         button1.pack()
         button2 = Button(topframe, text="how are you", fg="red")
         button2.pack(side=LEFT)
         button3 = Button(bottomframe, text="we are fine", fg="blue")
         button3.pack(side=RIGHT)
         root.mainloop()          

2 个答案:

答案 0 :(得分:0)

您需要将两个按钮都包装到tk.LEFT的{​​{1}}中:

topframe

import tkinter as tk if __name__ == '__main__': root = tk.Tk() topframe = tk.Frame(root) topframe.pack() bottomframe = tk.Frame(root) bottomframe.pack(side=tk.BOTTOM) button1 = tk.Button(topframe, text="hello boys", fg="green") button1.pack(side=tk.LEFT) button2 = tk.Button(topframe, text="how are you", fg="red") button2.pack(side=tk.LEFT) button3 = tk.Button(bottomframe, text="we are fine", fg="blue") button3.pack(side=tk.RIGHT) root.mainloop() 具有与button3.pack(side=tk.RIGHT)相同的效果,因为此元素是button3.pack()中的唯一小部件

答案 1 :(得分:0)

将tkinter导入为tk

如果名称 =='主要':

root = tk.Tk()

topframe = tk.Frame(root)
topframe.pack()

bottomframe = tk.Frame(root)
bottomframe.pack(side=tk.BOTTOM)

button1 = tk.Button(topframe, text="hello boys", fg="green").place(x=140, y=380)
button2 = tk.Button(topframe, text="how are you", fg="red").place(x=140, y=380)


button3 = tk.Button(bottomframe, text="we are fine", fg="blue").place(x=140, y=380)

root.mainloop()