How to Display Multiple Labels in One Line with Python Tkinter? - python

I have a Program that displays text on GUI Screen with Multiple Labels. But all the Labels are showing text in the new line and I want to show text in single line. Code is Below:
from tkinter import *
import tkinter as tk
win = Tk()
win.title("Label Screen")
win.geometry("800x600+50+50")
win.config(bg='white')
label1=Label(win, text="Label 1", font=("Calibri",24,"bold"), bg='white')
label1.pack(pady=15)
label2=Label(win, text="Label 2", font=("Calibri",24,"bold"), bg='white')
label2.pack(pady=15)
label3=Label(win, text="Label 3", font=("Calibri",24,"bold"), bg='white')
label3.pack(pady=15)
win.mainloop()
Output:

By default, the pack() method packs to the top, so you'll get a vertical stack of labels by default. You want to pack to the left to easily get all the labels on the same row. To do that, change:
labelX.pack(pady=15)
to
labelX.pack(side=tk.LEFT, pady=15)
Also, it's not ideal to import tkinter twice. Best to import it just once, as
import tkinter as tk
and then make sure to use tk. before all the methods, attributes, and classes.

Do this :
label1.pack(side=tk.LEFT,pady=15)
label2.pack(side=tk.LEFT,pady=15)
label3.pack(side=tk.LEFT,pady=15)
Further read : https://effbot.org/tkinterbook/pack.htm

Related

How to hide scrollbar when Text widget is not scrollable in tkinter

I am writing a program in python in which I want the Text widget in tkinter to have a scrollbar .
this is a part of my code -
import tkinter as tk
import tkinter.font as tkfont
window = tk.Tk()
display_text = "START\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEND"
font_1 = tkfont.Font(family='Helvetica', size=22, weight='bold')
my_label_1 = tk.Text(window, wrap=tk.WORD, bd=0, fg="white", bg="black", font=font_1, width=76, height=21,)
_scrollbar = tk.Scrollbar(window, command=my_label_1.yview)
my_label_1.configure(yscrollcommand=_scrollbar.set)
my_label_1.tag_configure("center", justify='center')
my_label_1.insert(1.0, display_text)
_scrollbar.pack(side=tk.RIGHT, ipadx=3, ipady=500, pady=(0, 48))
my_label_1.pack()
window.mainloop()
But I have 500 strings which I import from a csv file, when I put the strings in this code the strings go in disply_text variable, this works fine.
But for some of those strings the text widget is big enough and the scrollbar is not needed as the text widget is not scrollable for those. but the scrollbar still shows up it is disabled though but I don't want it to be there.
I know pack_forget() or grid_forget() can do this job, But Then what parameter should I put so I know that this iteration of text is not scrollable on the Text widget.
And my window is of fixed size i.e. window.resizable(False, False).

Python - Printing to GUI instead of terminal

Very new to Python here, and I'm trying to create a GUI app that returns a random recipe. Currently the print happens at the terminal, and I'd like it to print in the GUI instead.
from tkinter import *
import os
import random
root = tk.Tk()
def printRecipes():
recipes = [
"Tom Yum Soup",
"Carnitas",
"General Tso's Chicken"
]
print(random.choice(recipes))
canvas = tk.Canvas(root, height=600, width=700, bg="#A8D1BB")
canvas.pack()
magic = tk.Button(root, text="Print", padx=10, pady=5, fg="white", bg="black", command=printRecipes)
magic.pack()
root.mainloop()
This doesn't work, as most of you already know. I've read that I need to use a label or text for it, but the example I've found all involved static print statements like
label = Label(root,text="Recipe")
label.pack
To "print" a value to a GUI Window, a Label is used. .config() is a useful function. It is used to configure the provided widget.
Below magic.pack(), add this code. Notice that there is no text parameter. We will use that later.
label1=Label(root,pady=10,font=("arial",15))
label1.pack()
Next, in the function, where you had print(random.choice(recipes)), we will add:
label1.config(text=random.choice(recipes))
Notice that we used .config() and the text parameter. We configured the label, and added some text to it.
You need a tk.StringVar for this, i.e. a variable which you can change and the label can read from:
label_str = tk.StringVar(value="Some text")
label = tk.Label(root, textvariable=label_str)
Then, to update the value of this label and show it on the GUI:
label_str.set("New text")
label.update()

Prevent scrolledtext from taking up entire parent window disallowing other widgets from showing up

So I am actually writing a simple GUI program which makes use of ScrolledText widget from tkinter.scrolledtext module.
The problem is this ScrolledText widget seems to take up the complete space available in the parent window. It disallows me from putting in any other widget in the same parent window. I have tried using both grid and pack GeoManagers (i know place isn't very useful in all cases), but the other widgets won't show up (neither above the scrolledtext widget nor below it).
HERE IS THE CODE--
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.grid(row=0, column=0)
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)
tk.Button(win2, text='Got It', command=win2.destroy).grid(row=1, column=0)
This code is actually a part of a static method (i don't think makes a difference). When this is run the only thing visible on the screen is the scrolledtext widget with those 60 lines (i have tried it with 2 lines as well - still doesn't work).
The same happens when using pack().
To my surprise the only thing i could find in documentation is this::
ScrolledText Documentation
I don't know what I am missing here so please suggest me a way around this.
Thanks You :)
Solution with grid
The problem is the configuration of the grid: by default, the grid cells expand to fit the content. In your case the text widget is so big that the button in the row below is out of the screen. To fix that, you need to configure the first row and column to stretch with the GUI:
win2.rowconfigure(0, weight=1)
win2.columnconfigure(0, weight=1)
and make the text widget fill the cell, using the sticky option:
txtbox.grid(row=0, column=0, sticky='ewns')
This way the text widget will adapt to the window size and not the other way around.
Full code:
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
win2.rowconfigure(0, weight=1)
win2.columnconfigure(0, weight=1)
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.grid(row=0, column=0, sticky='ewns')
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)
tk.Button(win2, text='Got It', command=win2.destroy).grid(row=1, column=0)
Alternative method, using pack
You can use pack with the options fill='both' and expand=True to achieve the same result as with grid. In this case, the additional trick is to pack the button first to ensure that it has enough space to show in the window. Code:
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
tk.Button(win2, text='Got It', command=win2.destroy).pack(side='bottom')
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.pack(fill='both', expand=True)
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)

Make a Tkinter element on top of the others in Python

I'm searching for a way to put one element of Tkinter on top of the others in Python.
In the example below, what I tried to do is to put the yellow label on top of the white one - so when we change the size of the window the first label that will disappear is the white one, and only when its no longer visible the yellow one will start to shrink.
How can I realize this idea? Thanks in advance! (;
The code:
import tkinter as tk
root = tk.Tk()
root.config(background='black')
regularlabel = tk.Label(root, width=20, height=10, text='label')
regularlabel.pack()
bottomlabel = tk.Label(root, text='bottom label', bg='yellow')
bottomlabel.pack(side='bottom', fill='both')
root.mainloop()
Tkinter will shrink widgets in the reverse order of which they were added. Pack the bottom window first to solve your problem.
Since the layout of widgets tends to be changed more than the actual widgets during development, I find the code easier to understand and easier to maintain when layout is separate from widget creation. In the following example I've grouped the two layout statements together.
import tkinter as tk
root = tk.Tk()
root.config(background='black')
regularlabel = tk.Label(root, width=20, height=10, text='label')
bottomlabel = tk.Label(root, text='bottom label', bg='yellow')
bottomlabel.pack(side='bottom', fill='both')
regularlabel.pack()
root.mainloop()

Python - is there any way to expand the resolution ratio for existing Tkinter Tk() and Toplevel() widget? (zooming-in)

I just made an app using python and tkinter widgets.
There are Labels, Frames, Buttons, etc in the Tk and Toplevel widgets.
However, it includes thousands of codes and its really annoying to resize every widgets when I support multiple resolutions.
Is there any way to expand the resolution ratio for existing Tkinter Tk() and Toplevel() widget and their child widgets? (zooming-in)
If not, what would be the best approach to support multiple resolutions of a python app with the same ratio?
Any help would be much appreciated, sorry for bad English.
Yes, this is possible however it depends on the geometry manager you have used in your program.
For the .pack() method (which is arguably the simplest geometry method for "intelligent" GUI designs) you can use a range of attributes on when you declare .pack() on the widget. These attributes include (but are not limited to) fill, expand, anchor, padx, pady, etc.
The below shows an example of a set of three buttons which will automatically expand to fit the window if it changes or is initialised to a different size than was used during development.
from tkinter import *
root = Tk()
btn1 = Button(root, text="btn1")
btn2 = Button(root, text="btn2")
btn3 = Button(root, text="btn3")
btn1.pack(fill="both", expand=True)
btn2.pack(fill="both", expand=True)
btn3.pack(fill="both", expand=True)
root.mainloop()
For the .grid() method you will need to make use of the functions Grid.columnconfigure() and Grid.rowconfigure. Both of these have the attribute weight which determines which rows and columns should be given priority for assignment of extra space if more becomes available in the window. Setting all rows and columns to have a weight of 1 means they will all be given space equally. You will also need to use the sticky attribute when declaring .grid() on the widgets.
The below shows an example of a set of three buttons which will automatically expand to fit the window if it changes or is initialised to a different size than was used during development.
from tkinter import *
root = Tk()
for column in range(3):
Grid.columnconfigure(root, column, weight=1)
for row in range(1):
Grid.rowconfigure(root, row, weight=1)
btn1 = Button(root, text="btn1")
btn2 = Button(root, text="btn2")
btn3 = Button(root, text="btn3")
btn1.grid(column=0, row=0, sticky=N+S+E+W)
btn2.grid(column=1, row=0, sticky=N+S+E+W)
btn3.grid(column=2, row=0, sticky=N+S+E+W)
root.mainloop()
Using .place() would be a lot more difficult, you would need to have a function setup which would trigger on every window resize event which would calculate the size that the buttons need to expand to.
This would look something like the below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.button = Button(self.root, text="Button")
self.button.place(relx=0.5, rely=0.5, anchor="center")
self.root.bind("<Configure>", self.resize)
def resize(self, *args):
self.button.configure(width=self.root.winfo_width(), height=self.root.winfo_height())
root = Tk()
App(root)
root.mainloop()
Subjectively speaking, .pack() tends to be easier, however this all comes down to how much effort you're willing to put in to implement this with your current program.
Can't comment so I add a short tip to the detailed Ethan answer. You can design most of the GUIs in tkinter with either pack, grid or a combination of both (by placing frames on a window with one of them, and using either grid or pack inside of each frame, to "place" the widgets). You can tune the configurations for proper location and size when the window resizes. Keep placer use for special cases (like overlaying some widget on the top of others)

Categories

Resources