How to fix the Entry box when adjusting height in tkinter? - python

So I have a issue for where I type it will be right at the middle which I want to start it at the beginning of the box.
entry_notfor = Entry(root, textvariable=number_notoffer, width=30, bg="#1f1e1e", fg="#ffffff")
entry_notfor.place(x=5, y=390, height=100)
this is what it looks like now:

There's nothing you can do if you make the entry widget taller than it wants to be. An Entry widget is designed to be only one line tall. If you stretch it to be unnaturally tall, the text will always only appear centered vertically.
If your intent is to support multiline input then you should use a Text widget rather than an Entry.

Related

I can't for the life of me get this ttk / tkinter separator to expand

I have to write a program for some coursework I'm doing, and I chose to do an A-Level revision game to help others. I'm trying to get use a ttk separator to split up the area of the window where the game part occurs, and the area of the window where the question sits. My window is split up into 3 frames; one with 4 labels in, one with 4 buttons in and one with a ttk.Separator widget and a label. I cannot get the separator however to span the whole window.
I've been looking around and testing things, but nothing seems to work. I've looked at these two previous posts in terms of on this website:
ttk.Separator set the length/width
A Label in a Frame in a window won't stretch, why?
but neither of these solutions seemed to fix my problem, and I'm now out of ideas. Any help would be greatly appreciated.
main_revision_game_question_frame = Frame(parent_window, bg='#c2f0f0')
main_revision_game_question_frame.pack()
main_revision_game_question_frame.grid_propagate(1)
main_revision_game_question_frame.config(width=screen_width, height=40)
separator = ttk.Separator(main_revision_game_question_frame, orient=HORIZONTAL)
separator.grid(column=0, row=0, sticky='ew')
question_label = Label(main_revision_game_question_frame,
text=' Placeholder text for a question goes here!
\n######################################################',
bg='#c2f0f0', fg='#ff2824', font='"Open Sans" 26 bold')
question_label.grid(column=0, row=1, sticky='ew')
main_revision_game_question_frame.grid_columnconfigure(0,weight=1)
main_revision_game_question_frame.grid_rowconfigure(0, weight=1)
This is the code I've ended up with upon trying to combine a few solutions, and this is how it looks:
You can see the separator is there, but it does not stretch across the frame. Thank you for reading.
The separator is expanding to fill the frame, but the frame hasn't been configured to fill the space it has been given.
You need to expand the frame. One way to do that is to use the fill option of pack:
main_revision_game_question_frame.pack(fill="x")
Try to use
main_revision_game_question_frame.pack(fill="both")
so the frame fills the whole space it is given

Color lines seperately in tkinter?

I'm trying to write a music program that would display Chordpro files in python. Similar to this image, I want the chords, comments, and lyrics to each have different colours. I've tried these widgets:
I tried separating chords, comments and lyrics into multiple strings that could overlap on a canvas (with a different colour for each string) to make the full song, but sadly whitespace overrides previously rendered text, so I could only see the last layer.
Label/Message doesn't have functionality for multiple colours unless make a label for each line, which is very tedious, considering I want the font size to be adjustable too.
Text is editable, which I don't want.
Is there some kind of module or other tkinter widget that would allow separately coloured lines?
Just draw the separate lines at different y positions (heights) on the canvas. It's the first two parameters of the create_text() function. E.g.
self.canvas = Canvas(root, width=800, height=650, bg = '#afeeee')
self.canvas.create_text(100,10,fill="darkblue",font="Times 20 italic bold",
text="Greensleeves are my...")
So here, change 10 to the line position you want etc. Code copied from Python: how to add text inside a canvas?

wrap text in an entry widget in tkinter

I want to create an interactive text box in a tkinter GUI, the text box should get the text to wrap to the next line after the length of 30 characters, like it would do using the wraplength=30 attribute in a label widget. I am trying to get it to work using an Entry widget, this is what I am aiming for (apart from the wraplength attribute needs to be changed to something that works in an Entry widget:
ent = Entry(root, width=30, wraplength=30)
I also need to be able to make the Entry widget taller than one line, is there a way i can do that, for example making it vertically fill a frame (similarly to expand=True making it horizontally fill a frame).
Thank you!
I believe that Entry widgets are single line only, you may want to try Text widget
https://tkdocs.com/tutorial/morewidgets.html#text
The entry widget doesn't support wrapping. If you want to have multiple lines -- even if it's one long line that's wrapped -- you'll need to use either a Text, Label, or Message widget. Only the Text widget supports user input, the other two are strictly for display.
As for making the entry widget taller, you can do that with a geometry manager. For example, you can use the sticky option of grid or the fill and expand options of pack. This will make the widget taller, but the text will still just appear as a single line.
but text can't use (show="")

Best Module to Layout Text in Python

I am looking to write a program which outputs a bunch (around 30) of boxes like in this image:
I have been researching for weeks, I thought that to layout text in a graphically pleasing way I could use QTableWidget in PyQT, but I now realise that it is far too difficult to learn for such a simple task, there must be a quicker way. So I am thinking now to pass to Tkinter or maybe just draw the information with a Drawing Module like PyCairo and only then place each image in a PyQT interface. Sorting out all the positioning in a drawing module would be much quicker than learning how to do the same in PyQT.
But I feel I am missing something, I would have thought a much easier task to layout in a nice way a bunch of numbers in a repetitive format.
Some of the boxes will need also some graphical content has bars and charts for which I though to use plotly or cairo.
Whilst you're probably better off doing this with HTML and CSS as has been mentioned above, this isn't too difficult to do with Python and can be achieved with the use of just tkinter. Please see my code below for an example of how this could work:
from tkinter import *
root = Tk()
frame1 = []
frame2 = []
c = 0
numberofboxes = 8 #change this to increase the number of boxes
for i in range(numberofboxes):
if i % 4 == 0: #checks if the current box is the fourth in row
c = c + 1 #if the current box is the forth in the row then this runs and increases a counter which we later use to determine the row
if len(frame1) != c: #checks if the number of rows currently existing matches the number there should be
frame1.append(Frame(root)) #if the numbers don't match this runs and creates a new frame which acts as another row
frame1[c-1].pack(expand="True", fill="both") #packs the new row
frame2.append(Frame(frame1[c-1], bg="green")) #this is where the boxes are created
frame2[i].pack(ipadx="50", ipady="50", side="left", padx="10", pady="10", expand="True", fill="both") #this is where the boxes are placed on the screen
for i in range(len(frame2)): #this for loop places the items inside each box, all of this can be replaced with whatever is needed
Label(frame2[i], text="CO"+str(i), bg="green", fg="white").pack(side="top", anchor="w")
Label(frame2[i], text="12165.1"+str(i), bg="green", fg="white").pack(side="top", anchor="w")
Label(frame2[i], text="+60.7"+str(i), bg="green", fg="white").pack(side="bottom", anchor="e")
Label(frame2[i], text="+1.2"+str(i)+"%", bg="green", fg="white").pack(side="bottom", anchor="e")
root.mainloop()
So essentially, we create a frame for each row and each box is a frame which has elements packed inside it and is fitted in the "row frame" 4 to each row.
You should take a close look at all of the options for .pack() during this script as well as they are necessary to achieve the desired layout and results.
For your triangles you would most likely need to either import an image or draw them within a canvas positioned properly or (as was pointed out by Bryan Oakley below) you could use unicode characters for the arrows, which would be an awful lot simpler.

Tkinter grid() with negative padding

Is there a way to add negative padding when placing a Tkinter widget in a grid ?
Things such as myWidget.grid(padx=-10) do not work.
I am trying to display a Radiobutton and a Label side-by-side, my problem is the gap between the radio's text and the label's text is too wide, hence my question on adding a negative padding to the label.
Code:
rb = Radiobutton(text="foo")
rb.grid(row=row, column=0, sticky=W)
l = Label(text="bar")
l.grid(column=1, sticky=W)
Thanks !
No, you cannot add negative padding. There are probably better ways to solve your problem. For example, since your label is to the right of the radiobutton, have you tried using the anchor option on the label so that the text is left-aligned?

Categories