EDIT: I found the solution! My xorg settings were all wacky because I have an ultrawide monitor, and it had no idea what size (and therefore dpi) my monitor was. Explicitly setting the display and font dpi in X's settings fixed the rendering in all GUI's.
ORIGINAL:
When I run any tkinter program, many of the widgets render at around 1/2 size, especially text. Why is this happening? My window manager is Window Maker, and I'm running the latest version of Tcl/Tk.
Any help will be greatly appreciated!
My code:
import tkinter as tk
window = tk.Tk()
label = tk.Label(text="Name")
entry = tk.Entry()
button = tk.Button(text="Submit")
label.pack()
entry.pack()
button.pack()
window.mainloop()
Window manager: Window Maker 0.95.0
Tk: tk 8.6.10-2
Screenshot:
In tkinter you can only change the size of a label with the font atribute, and same goes for the text inside the button. The button size can be changed with the width and height atribute.
from tkinter import *
window = Tk()
label = Label(text="Name", font='Helvetica 15')
entry = Entry()
button = Button(text="Submit",font ='Helvetica 15', height="3", width="10")
label.pack(pady = 5) # add pady inside the pack
entry.pack()
button.pack(pady = 5)
window.mainloop()
Related
I am trying to make a simple GUI for a school project. I am new to using tkinter. I used to create my GUIs using pygame, which is great for custumization, but not really for efficiency haha.
I am creating a button for each file in my /graphs folder. In this code, line 42, it seems to me I can't change the background color of the button. This is a reccurent problemI have with tkinter. I have no idea if it is something I am doing wrong or if there is a problem with the framework I am using.
I am trying to make the bg color red.
I am using MACOS, and i know there are complications with tkinter and mac, but i can't use tkmacosx because i need this project to be runnable on windows and linux as well.
Thank you for your help, don't hesitate if you have any suggestions on common practices with tkinter that i am not applying or if you have the solution to my problem !
Here is the output and the code
Output
import tkinter as tk
import os
# colors
SILVER = "#BFACAA"
BLACK = "#02020A"
OXFORD_BLUE = "#05204A"
WISTERIA = "#B497D6"
LAVENDER = "#E1E2EF"
RED = "#FF0000"
# Sizes
WIDTH = 800
HEIGHT = 600
# Path
PRJ_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
class Window:
def __init__(self):
self.window = tk.Tk()
self.window.title("Graph Scheduler")
self.window.geometry(f"{WIDTH}x{HEIGHT}")
self.window.configure(background=LAVENDER)
# Title and title box
title_box = tk.Frame(self.window, bg=SILVER, width=WIDTH)
title_box.pack(fill="x")
title = tk.Label(title_box, text="Graph Scheduler", font=("Arial", 40), bg=SILVER, fg=BLACK)
title.pack(pady=5)
# File bar
file_bar = tk.Frame(self.window, bg=OXFORD_BLUE, width=200, height=HEIGHT)
file_bar.pack(fill="y", side="left")
# File bar buttons
file_bar_buttons = tk.Frame(file_bar, bg=OXFORD_BLUE, width=200, height=HEIGHT)
file_bar_buttons.pack(fill="y", side="left")
for file in os.listdir(PRJ_DIR + "/graphs"):
if file.endswith(".txt"):
file_bar_button = tk.Button(file_bar_buttons, text=file,background=RED, fg=SILVER, font=("Arial", 20), width=10, height=2)
file_bar_button.pack(pady=5)
self.window.mainloop()
Window()
use from tkmacosx import Button
I am not able to figure out why widgets are not visible when ".place" is used. It works when ".pack" is used. My GUI needs precision positioning of widgets and hence I need to use ".place". Am I doing anything wrong?
Environment: Python: 3.9.6 Windows 10 (21H1)
import tkinter as objTK
from tkinter import ttk as objTTK
root = objTK.Tk()
root.title("Tab Widget")
tabControl = objTTK.Notebook(root)
objSummaryTab = objTTK.Frame(tabControl)
objSettingsTab = objTTK.Frame(tabControl)
tabControl.add(objSummaryTab, text ='Summary')
tabControl.add(objSettingsTab, text ='Settings')
lb1 = objTTK.Label(objSummaryTab, text ="Summary")
lb1.place(x=5, y=5)
lb2 = objTTK.Label(objSettingsTab, text ="Settings")
lb2.place(x=5, y=5)
tabControl.place(x=5, y=5)
root.bind("<Escape>", lambda _: root.destroy())
root.geometry("500x500")
root.mainloop()
This error is because place() does not automatically resize the widgets, like pack() and grid() do. You need to specify width and height arguments for tabControl: tabControl = objTTK.Notebook(root, width=100, height=100), for example. In this case, width and height are in pixels.
I am trying to create a translucent window in Tkinter like the one in windows 11
How to do this? If we cannot do this can we capture a part of a screen and blur it using cv2 and use it as a continuously updating background?
No, this is not directly possible with tkinter. But:
If you use PIL, you can get the location of the window, and then take a screenshot, then blur it and then make it your app background. But this wont work if user tries to move/resize the application. But here is a rough code:
from tkinter import *
from PIL import ImageTk, ImageGrab, ImageFilter # pip install Pillow
root = Tk()
root.overrideredirect(1) # Hide the titlebar etc..
bg = Canvas(root)
bg.pack(fill='both',expand=1)
root.update()
# Get required size and then add pixels to remove title bar and window shadow
left = root.winfo_rootx()
top = root.winfo_rooty()
right = left + root.winfo_width()
bottom = top + root.winfo_height()
root.withdraw() # Hide the window
img = ImageGrab.grab((left,top,right,bottom)) # Get the bg image
root.deiconify() # Show the window
img = img.filter(ImageFilter.GaussianBlur(radius=5)) # Blur it
img = ImageTk.PhotoImage(img)
bg.create_image(0,0, image=img, anchor='nw') # Show in canvas
label = Label(root,text='This is a translucent looking app')
bg.create_window(bg.winfo_width()/2,bg.winfo_height()/2,window=label) # Position in the center
root.mainloop()
Output with tkinter:
tkinter is not the best choice if you are trying to go for a modern look, use PyQt and check qtacrylic
Output with PyQt:
For live blur (native Windows blur) use "BlurWindow":
python -m pip install BlurWindow
from tkinter import *
from ctypes import windll
from BlurWindow.blurWindow import blur
root = Tk()
root.config(bg='green')
root.wm_attributes("-transparent", 'green')
root.geometry('500x400')
root.update()
hWnd = windll.user32.GetForegroundWindow()
blur(hWnd)
def color(hex):
hWnd = windll.user32.GetForegroundWindow()
blur(hWnd,hexColor=hex)
e = Entry(width=9)
e.insert(0,'#12121240')
e.pack()
b = Button(text='Apply',command=lambda:[color(e.get())])
b.pack()
root.mainloop()
Is there any way to resize button size in python tkinter?
I have tried to resize button size in python 3.7.2 tkinter by using button.config(width = 100, hight = 100), but it didn't work properly. Is there any way to resize button?
I use Python 3.7.2 and Windows 10.
import tkinter as tk
win = tk.Tk()
#*** Settings ***#
win.title("Project_title")
win.geometry("660x450")
win.resizable(False, False)
wall = tk.PhotoImage(file = "pictures_gui.gif")
wall_label = tk.Label(image = wall)
#*** Settings ***#
#*** Test code ***#
def click_me():
button.configure(text="** I have been clicked")
button = tk.Button(win,text = "Click me!",command=click_me)
button.grid(column=1, row=0)
button.config(width = 100,hight = 100)
#*** Test code ***#
win.mainloop()
I'm guessing you want to set the button size in pixels. The button size defaults to characters when the button displays text but no image. To get the size to be pixels you have to display an image in the button. See the example below:
import tkinter as tk
win = tk.Tk()
win.geometry("660x450")
win.resizable(False, False)
def click_me():
button.configure(text="** I have been clicked")
# Create a transparent image to allow Button size in pixels
pixel = tk.PhotoImage(file='images/pixel.png')
button = tk.Button(win, text="Click me!", command=click_me,
image=pixel, compound='center')
button.grid(column=1, row=0)
button.config(width=100, height=100) # Config size in pixels
win.mainloop()
The pixel.png image is 1x1 pixel and has transparent color.
I am using Python 3.6 on a mac, in view of creating controls for a heating system using a Raspberry Pi 3. I have created a master Tk window with a canvas in it to display graphics for on/off times. I am using an OptionMenu in a top-level popup window with overridedirect to remove the window decorations. I wish to also use overridedirect on the master Tk window, but when I use these two instances of overridedirect, my OptionMenu ceases to work(it works with just one use of overridedirect). The OptionMenu is displayed in the popup window, but does not drop down when clicked on. It is quite a long program, so I haven't included any of my code as I'm unsure what would be relevant. Any advice would be greatly appreciated!
Didn't think I could do it, which is why I didn't include code(kept getting errors), but managed it in the end!
from tkinter import *
master = Tk()
master.overrideredirect(1) # remove window border
master.geometry('800x480')
canvas_window = Canvas(master, width=800, height=305)
outer_rect = canvas_window.create_rectangle(39, 3, 760, 304, fill="BLUE")
close_button = Button(master, text="close", command=master.destroy)
close_button.pack()
def menu_sel_trg(self):
print("Menu Triggered")
def popup(*self):
popup_win = Toplevel()
popup_win.wm_overrideredirect(1) # remove window border
popup_win.geometry('250x250')
on_hrs_options = range(0, 24, 1)# sets range of list
on_hrs_variable = StringVar(popup_win)
on_hrs_variable.set(1)# menu default value
menu_on_hours = OptionMenu(popup_win, on_hrs_variable, *on_hrs_options, command=menu_sel_trg)
menu_on_hours.pack()
close_popup = Button(popup_win, text="Close", command=popup_win.destroy)
close_popup.pack()
canvas_window.tag_bind(outer_rect, '<Button-1>', popup)
canvas_window.pack()
master.mainloop()