12102

How to line left justify label and entry boxes in Tkinter grid

<h3>Question</h3>

I'm still pretty new to Tkinter and Classes, but I am trying to left justify labels and entry boxes each within their own column of a Tkinter grid. I am using Justify=LEFT, but it seems to have no impact as the labels look centered and the entry boxes start where the label ends.

from Tkinter import * class LabeledEntry(Frame): def __init__(self, parent, *args, **kargs): text = kargs.pop("text") Frame.__init__(self, parent) Label(self, text=text, justify=LEFT).grid(column=0,row=0) Entry(self, justify=LEFT, *args, **kargs).grid(column=1, row=0) class User_Input: def __init__(self, parent): fields = ['Text Label 1', 'This is the text Label 2'] GUIFrame =Frame(parent) GUIFrame.pack(expand=True, anchor=NW) parent.minsize(width=350, height=325) field_index = 1 for field in fields: self.field = LabeledEntry(GUIFrame, text=field) self.field.grid(column=0, row=field_index) field_index += 1 self.Button2 = Button(parent, text='exit', command= parent.quit) self.Button2.place(x=25, y=300) root = Tk() MainFrame =User_Input(root) root.mainloop()
<h3>Answer1:</h3>

I think your problem lies in the fact that each time you create a new instance of LabeledFrame, you are placing both the Entry & Label within the same Frame.

The grid settings for this Frame are separate from any other Frame, so it is impossible for LabeledFrames to align columns as they do not have the same values for column widths.

Normally to accomplish what you are after you would simply put sticky = W in the grid options for the Entry widget to left-justify the contents of the cell. However, this will only work for each individual Frame, leaving the contents of each separate LabeledFrame out of alignment.

Easiest way to fix this without changing much code:

You'll want to add a line to your for loop. If you specify a large minimum-width of the column that self.field's Frame is inserted into, you can be sure that things will align how you want them to. I've also added config options to the grid calls within the LabeledEntry class: sticky = W for the Label & sticky = E for the Entry.

Try this out and see if it solves your problem. If you would like the column to take less space simply reduce minsize.

from Tkinter import * class LabeledEntry(Frame): def __init__(self, parent, *args, **kargs): text = kargs.pop("text") Frame.__init__(self, parent) Label(self, text=text, justify=LEFT).grid(sticky = W, column=0,row=0) Entry(self, *args, **kargs).grid(sticky = E, column=1, row=0) class User_Input: def __init__(self, parent): fields = ['Text Label 1', 'This is the text Label 2'] GUIFrame =Frame(parent) GUIFrame.pack(expand=True, anchor=NW) parent.minsize(width=350, height=325) field_index = 1 for field in fields: self.field = LabeledEntry(GUIFrame, text=field) self.field.grid(column=0, row=field_index) self.field.grid_columnconfigure(index = 0, minsize = 150) field_index += 1 self.Button2 = Button(parent, text='exit', command= parent.quit) self.Button2.place(x=25, y=300) root = Tk() MainFrame =User_Input(root) root.mainloop()
<h3>Answer2:</h3>

I have had success using both justify and anchor:

Label(self, text=text, justify=LEFT, anchor="w").grid(sticky = W, column=0,row=0)

来源:https://stackoverflow.com/questions/31140590/how-to-line-left-justify-label-and-entry-boxes-in-tkinter-grid

Recommend

  • How to line left justify label and entry boxes in Tkinter grid
  • JSON array to ExpandoObject via JSON.NET
  • Comparing DateTimes with Unspecified and UTC kinds
  • Behavior when dereferencing the .end() of a vector of strings
  • concat two int values in postgresql
  • Custom string Comparison in C#
  • C++ bitwise copy of object failing? Why?
  • Grep only one of partial duplicates
  • SQL Server Delete all rows more than 10 minutes old
  • How to download Appium Desktop in Ubuntu 16
  • Understanding foreign keys
  • Can't copy sqlite database from assets folder in Android
  • R: Creating a new column using another dataframe
  • uploading images in codeigniter, is userfile required?
  • What is the common solution for multiple datasources?
  • Implicit resolution with covariance
  • remove question mark from 301 redirect using htaccess when the user enters the old URL
  • Correct way to send X11 mouse events
  • TortoiseSVN merging a branch to trunk
  • Is it possible to use LinkShare signatures in an iOS Smart Banner?
  • Zip library for Windows Phone 7
  • connect.cookieParser and connect.session
  • No Gradle file syntax highlighting in Eclipse Mars.2
  • Custom progress dialog not working
  • Validating my form with Jquery
  • Allowing audio files in Spring MVC 3.0?
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • How do I use libcurl to printf a remote FTP directory listing?
  • C++ STL stack pop operation giving segmentation fault
  • How to clear a browser cache in Protractor
  • How to use Streams api peek() function and make it work?
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • Using Service Component Runtime
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • ReferenceError: TextEncoder is not defined