I am trying to resize a window based on how much text there is/are in a/some label(s). Here is what I mean: https://imgur.com/a/kBb1xaQ. I want this window to get as big to fit all the text inside.
I have been looking for parameters from the Label from tkinter but nothing seemed to work. Some help would be much appreciated, thank you.
This is some of the code I have done:
import tkinter
from tkinter import *
header = 'Staff requirements'
text = ['bla bla', 'bla bla']
window = Tk()
window.title('{}'.format(header.capitalize()))
window.geometry('400x200') # 80 + (x*15)
title_frame = Frame(window, borderwidth=2, relief='solid')
title_frame.pack(pady=10)
title_lbl = Label(title_frame, text=header.capitalize(), padx=5, pady=5, borderwidth=1, relief='solid', font=('Calibri', 15))
title_lbl.pack()
content_frame = Frame(window, borderwidth=1, relief='solid')
content_frame.pack(pady=10, padx=10, fill='y')
for sub_text in text:
content_lbl = Label(content_frame, text=sub_text, padx=5, pady=5, wraplength=360, justify='left', font=('Calibri', 10))
content_lbl.pack()
Change:
window.geometry('400x200')
to :
window.geometry('')
Also , if you want to prevent the user from resizing the window , you can try :
window.resizable(False,False)
I believe you are trying to find the Entry method, be careful with the geometry...
import tkinter as tk
guiApp= tk.Tk()
guiApp.geometry("630x630")
textPlace= tk.Entry(guiApp)
textPlace.place(x = 10,y = 10,width=600,height=600)
guiApp.mainloop()
Related
I wanted to make a title label that spans the top of the screen with the text in the middle. this works when the window opens but if I go into fullscreen, the label only spans half the length. if I make the label bigger, the text is not in the middle. it's a win-lose situation. any way to make both of them work?
import tkinter as tk
from tkinter import *
Title_Font = ("Hallo Sans", 20, "bold")
MyBlue = '#30D5C8'
Home = tk.Tk()
Home.title("Guitar Bud")
Home.geometry('1000x700')
Home.configure(bg='grey')
ExersisesLbl = tk.Label(Home, width=60, height=1, bg='black', fg=MyBlue, text='Exersises', font=Title_Font, anchor=CENTER)
ExersisesLbl.grid(row=0, column=0, columnspan=5, sticky='ew')
like #JacksonPro said, use grid_columnconfigure().
import tkinter as tk
from tkinter import *
Title_Font = ("Hallo Sans", 20, "bold")
MyBlue = '#30D5C8'
Home = tk.Tk()
Home.title("Guitar Bud")
Home.geometry('1000x700')
Home.configure(bg='grey')
Home.grid_columnconfigure(0, weight=1)
ExersisesLbl = tk.Label(Home, width=60, height=1, bg='black', fg=MyBlue, text='Exersises', font=Title_Font, anchor=CENTER)
ExersisesLbl.grid(row=0, column=0, columnspan=5, sticky='ew')
mainloop()
The code below represents my first steps into making a calculator on python using tkinter. The idea is to put the numbers on a grid accordingly, and then make the all of the necessary adjustments. The problem here is that I get the following error:
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
I'm aware that this is because of the canvas.pack(), but isn't it necessary for the background? How can I separate them in the most efficient way possible? On that note, is there a way to put all of the buttons/grids together using fewer lines of code? Thanks in advance.
from tkinter import *
#Creating the window function (?)
window = Tk()
#Creating a frame and a background for the calculator
canvas = tk.Canvas(window, height=700, width=700, bg="#83CFF1")
canvas.pack()
frame = tk.Frame(window, bg="white")
frame.place(relwidth=0.7, relheight=0.7, relx=0.15, rely=0.15)
#Creating the buttons for the calculator
button1 = Label(window, text="1")
button2 = Label(window, text="2")
button3 = Label(window, text="3")
button4 = Label(window, text="4")
button5 = Label(window, text="5")
button6 = Label(window, text="6")
button7 = Label(window, text="7")
button8 = Label(window, text="8")
button9 = Label(window, text="9")
button0 = Label(window, text="0")
#Adding it to the screen
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=0, column=2)
button4.grid(row=1, column=0)
button5.grid(row=1, column=1)
button6.grid(row=1, column=2)
button7.grid(row=2, column=0)
button8.grid(row=2, column=1)
button9.grid(row=2, column=2)
button0.grid(row=3, column=1)
#Ending the loop (?)
window.mainloop()
Create buttons using Python list comprehension.
For the grid placment use i // 3 (floor division) and i % 3 (modulo) inside a for loop.
Then just simply add the last button manually.
This code below will do the trick:
import tkinter as tk
window = tk.Tk()
frame = tk.Frame(window, bg="white")
frame.place(relwidth=0.7, relheight=0.7, relx=0.15, rely=0.15)
#Creating the buttons for the calculator
buttons = [tk.Button(frame, text = i) for i in range(1, 10)]
for i, button in enumerate(buttons):
button.grid(row = i // 3, column = i % 3)
#Add last button 0
buttons.append(tk.Button(frame, text = 0))
buttons[-1].grid(row=3, column=1)
window.mainloop()
So I created a frame in which I want to put two widgets with them being centered on the x-axis. When it's one object pack() centers it automatically. But I can't figure out two widgets. I tried with grid() but there is free space left on the right which makes it look unsymetrical as seen in the image.
how could I get what is seen on the right? (I'd prefer it being dont with pack() but if there is a solution with grid() and/or place() as well i'd appreciate those as well!)
here's the code for the left picture
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.place(relwidth=0.5, relheight=0.5, relx=0.5, rely=0.5, anchor=CENTER)
label = Label(frame, bg="lime", text="label", font=font.Font(size=20))
label.grid(column=0, row=0)
button = Button(frame, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.grid(column=1, row=0)
root.mainloop()
You can use frame.pack() to easily position the frame in the top, middle of its parent.
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.pack()
label = Label(frame, bg="lime", text="label", font=font.Font(size=20))
label.grid(column=0, row=0)
button = Button(frame, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.grid(column=1, row=0)
root.mainloop()
You can put the label and button in another frame, and use pack() on that frame:
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.place(relwidth=0.5, relheight=0.5, relx=0.5, rely=0.5, anchor=CENTER)
frame2 = Frame(frame)
frame2.pack() # default side='top'
label = Label(frame2, bg="lime", text="label", font=font.Font(size=20))
label.pack(side='left', fill='both')
button = Button(frame2, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.pack(side='left')
root.mainloop()
I need a python messagebox with custom buttons.
I need something like this. I need it to return the button clicked. Like If I clicked 'A' it would return 'A'
I know about tkinter but I want to use this with pygame and can't get it to work.
Here is what I made with tkinter, not the perfect messagebox, but why not?
For the theme used you have to install it first, like:
pip install ttkthemes
Then the code
# imports
from tkinter import *
from tkinter import ttk
from ttkthemes import themed_tk as tktheme
from PIL import ImageTk, Image
from tkinter import messagebox
# making new themed window
root = tktheme.ThemedTk()
root.title('Take selection')
root.get_themes()
root.set_theme('vista')
# message to be shown by the box
message = 'Here is a custom messagebox.'
# defining functions
def click1():
root.destroy()
return 'A'
def click2():
root.destroy()
return 'B'
def click3():
root.destroy()
return 'C'
# creating white frame
frame1 = Frame(height=139, width=440, bg='white')
frame1.grid(row=0, column=0)
# creating gray frame
frame2 = ttk.Frame(height=50, width=440)
frame2.grid(row=1, column=0)
# importing the image, any image can be used(for the question mark)
dir = Image.open('Blue_question.png')
dir = dir.resize((50, 50), Image.ANTIALIAS)
img_prof = ImageTk.PhotoImage(dir)
img_label = Label(root, image=img_prof, bg='white')
img_label.grid(row=0, column=0, sticky=W, padx=25)
# defining main label
cust_messagebox = Label(root, text=message, font=('Arial', 10), bg='white')
cust_messagebox.grid(row=0, column=0, sticky=W, padx=(95, 0))
# defining buttons
button1 = ttk.Button(root, text='A', command=click1)
button1.grid(row=1, column=0, sticky=W, padx=30, ipadx=10)
button2 = ttk.Button(root, text='B', command=click2)
button2.grid(row=1, column=0, ipadx=10)
button3 = ttk.Button(root, text='C', command=click3)
button3.grid(row=1, column=0, sticky=E, padx=(0, 30), ipadx=10)
# keeping the app alive
root.mainloop()
With bigger messages you might want to increase the width of the frames and the play with the padding of the widgets too.
If any doubts or errors, do let me know.
Cheers
I know there is a lot of info related to this question, but I cannot get any of it to work with my specific situation.
I'm trying to create a tkinter button menu out of gifs and I want these gifs to be flush up against one another, no border, no spacing. I've been able to achieve this with a regular button, but not with a button with an image file. I've attempted this with labels and there is no "border", but I still cant get rid of the padding, even after setting the relevant attributes to "0".
I'm aware of both the "bd" and the "highlightthickness" attributes and I'm pretty sure that the highlightthickness attribute is specific to the canvas widget, but I could be wrong? Either way, i've put it just about anywhere I can think and I still have this annoying border.
Can anyone point me in the right direction here? I'm a newbie to python and tkinter, but relatively comfortable with coding. This seems almost as straightforward as html tables, etc, but ive spent a couple nights scouring the internet and I can't find any answers, let alone any example of an image based button menu without spacing issues.
Apologies again for the newbie-ness of this dilemma. Any guidance will be greatly appreciated. Thanks!
code example using "Label" widget:
from Tkinter import *
root = Tk()
root.wm_title("Window Title")
root.config(background = "#FFFFFF")
leftFrame = Frame(root, width=600, height = 800, bd=0, highlightthickness=0)
leftFrame.grid(row=0, column=0, padx=0, pady=0)
try:
imageEx = PhotoImage(file = 'nav1.gif')
imageEx2 = PhotoImage(file = 'nav2.gif')
Label(leftFrame, image=imageEx).grid(row=1, column=0, padx=0, pady=0)
Label(leftFrame, image=imageEx2).grid(row=2, column=0, padx=0, pady=0)
except:
print("Image not found")
root.mainloop()
code example using "Button" widget:
import Tkinter as tk
button_flag = True
def click():
global button_flag
if button_flag:
button1.config(bg="white")
button_flag = False
else:
button1.config(bg="green")
button_flag = True
root = tk.Tk()
frame1 = tk.Frame(root)
frame1.pack(side=tk.TOP, fill=tk.X)
photo1 = tk.PhotoImage(file="nav1.gif")
button1 = tk.Button(frame1, compound=tk.TOP, width=320, height=55, image=photo1,
text="", bg='green', command=click, bd=0, highlightthickness=0)
button1.pack(side=tk.LEFT, padx=0, pady=0)
button1.image = photo1
frame2 = tk.Frame(root)
frame2.pack(side=tk.TOP, fill=tk.X)
photo2 = tk.PhotoImage(file="nav2.gif")
button2 = tk.Button(frame2, compound=tk.TOP, width=320, height=55, image=photo2,
text="", bg='green', command=click, bd=0, highlightthickness=0)
button2.pack(side=tk.LEFT, padx=0, pady=0)
button2.image = photo2
root.mainloop()