请帮助修复脚本。
from tkinter import *
colors = ['red', 'white', 'blue']
def packbox(root):
Label(root, text='Pack').pack()
for color in colors:
row = Frame(root)
lab = Label(row, text=color, relief=RIDGE, width=25)
ent = Entry(row, bg=color, relief=SUNKEN, width=50)
row.pack(side=TOP, expand=YES, fill=BOTH)
lab.pack(side=LEFT, expand=YES, fill=BOTH)
ent.pack(side=RIGHT, expand=YES, fill=BOTH)
root = Tk()
packbox(root)
mainloop()
我想在左边缘的标签小部件中对齐文本
答案 0 :(得分:10)
试试这个
Label(root, text='Pack', anchor='w').pack(fill='both')
答案 1 :(得分:3)
锚点用于定义文本相对于参考点的位置。
以下是可能的常量列表,可用于Anchor属性。
NW
N
NE
W
CENTER
E
SW
S
SE
答案 2 :(得分:2)
以下打开一个新窗口,其中包含每个按钮whitebutton,redbutton和 按下蓝色按钮时,按钮全部对齐LEFT,并在每个按钮的方法中 还有一个名为“关闭窗口”的按钮,用于关闭每次单击按钮时打开的新窗口。
from Tkinter import*
import Tkinter as tk
class Packbox(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
# Initialize buttons redbutton, whitebutton and bluebutton
whitebutton = Button(self, text="Red", fg="red", command=self.white_button)
whitebutton.pack( side = LEFT)
redbutton = Button(self, text="white", fg="white", command=self.red_button)
redbutton.pack( side = LEFT )
bluebutton = Button(self, text="Blue", fg="blue", command=self.blue_button)
bluebutton.pack( side = LEFT )
self.white_button()
self.red_button()
self.blue_button()
# Define each buttons method, for example, white_button() is whitebutton's method, which
# is called by command=self.white_button
def white_button(self):
self.top = tk.Toplevel(self)
# Creates new button that closes the new window that is opened when one of the color buttons
# are pressed.
button = tk.Button(self.top, text="Close window", command=self.top.destroy)
# prints the text in the new window that's opened with the whitebutton is pressed
label = tk.Label(self.top, wraplength=200,text="This prints white button txt")
label.pack(fill="x")
button.pack()
def red_button(self):
self.top = tk.Toplevel(self)
button = tk.Button(self.top, text="Close window", command=self.top.destroy)
label = tk.Label(self.top, wraplength=200,text="This prints red button txt")
label.pack(fill="x")
button.pack()
def blue_button(self):
self.top = tk.Toplevel(self)
button = tk.Button(self.top, text="Close window", command=self.top.destroy)
label = tk.Label(self.top, wraplength=200,text="This prints blue button txt")
label.pack(fill="x")
button.pack()
if __name__ == "__main__":
root = tk.Tk()
Packbox(root).pack(side="top", fill="both", expand=True)
root.mainloop()