2014-02-01 5 views
4

, пожалуйста, помогите исправить сценарий.как выровнять текст влево?

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

Я хотел бы, чтобы выровнять текст в метке виджета на левом краю

ответ

5

Попробуйте

Label(root, text='Pack', anchor='w').pack(fill='both') 
+0

спасибо. поэтому работает lab = Label (строка, текст = цвет, рельеф = RIDGE, width = 25, anchor = 'w') – Sergey

1

Следующие открывает новое окно с текстом для каждой из кнопок whitebutton, redbutton и синяя кнопка при нажатии, кнопки выравниваются влево, а в методе каждой кнопки есть дополнительная кнопка с названием «Закрыть окно», которая закрывает новое окно, которое открывается при каждом нажатии кнопки.

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

Якоря используются для определения места размещения текста относительно контрольной точки.

Вот список возможных констант, которые могут использоваться для атрибута Anchor.

NW 

N 

NE 

W 

CENTER 

E 

SW 

S 

SE 
Смежные вопросы