please help to fix the script.
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()
I would like to align the text in the Label widget on the left edge
Left-aligned text is text that is aligned with a left edge. You can also left-align your text with the shortcut Ctrl+L (Cmnd+L).
Try this
Label(root, text='Pack', anchor='w').pack(fill='both')
Anchors are used to define where text is positioned relative to a reference point.
Here is list of possible constants, which can be used for Anchor attribute.
NW
N
NE
W
CENTER
E
SW
S
SE
The following opens a new window with text for each of the buttons whitebutton, redbutton and bluebutton when they are pressed, the buttons are all aligned LEFT, and in each button's method there is an additional button named "Close window" that closes the new window that is opened with each button click.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With