Python tkinter button font is getting pixelated in bigger sizes - python

First of all, thank you for trying to help me out. I am currently programming my first GUI with tkinter and i try to create buttons with big fonts, cause i want to create a program for visually impaired people. Sadly I have two problems i can't solve with the internet on my own ..
This is the relevant part of my code: (sorry for the german variables)
import tkinter
from tkinter import *
from tkinter import font
import tkinter.messagebox
class Oberflaeche(tkinter.Frame):
def __init__(self, master=None):
tkinter.Frame.__init__(self, master)
self.pack()
MyFont = font.Font(family='times', size=50)
self.close_window = tkinter.Button(self, font=MyFont, text="Programm \nbeenden", command=self.close_window, bg="white", height = 3, width = 18, bd=3, relief="solid")
self.close_window.pack()
def close_window(self):
root.destroy()
root = tkinter.Tk()
root.title("Prototyp MVP")
root.minsize(width=300, height=300)
root.configure(background='white')
oberflaeche = Oberflaeche(master=root)
oberflaeche.mainloop()
when I try to change the font tkFont.Font is not working. There is the Error:
NameError: name 'tkFont' is not defined
That is the reason I tryed font.Font. But no matter how I change the font family or type, it always looks awful and pixelated....
Picture of the failed Button
I am using python 3.5.5, Ubuntu 16.04 and tk 8.6.8.

I am using Spyder(python 3.6) here is the result;
i.hizliresim.com/6NgZlW.jpg
Maybe because of version?
Actually your program is working succesfully ı did not change any codes ı tried on my pc and the result is perfect(Win10 Spyder(Python 3.6).)

i am also used to Jupyter notebook and i was trying with VS Code everything is working well. I suggest you trying to play around with scaling and let me know is it working for you?
Scaling of Tkinter GUI in 4k (3840*2160) resolution
Results
Jupyter
VS Code

Related

Python Tkinter color not working, it is the same whether I change the customization with canvas

I apologize in advance for any ignorant mistakes I make, I am still a beginner.
I recently started using tkinter on python, and while I tried using the backround customization in tkinter, It does not change any colors in the backround, the same goes with the frame. below is the code I have written.
import tkinter
import tkinter as tk
class GUI:
root = tkinter.Tk()
canvas = tk.Canvas(
height=700,
width=700,
bg="#263D42"
)
canvas.config(bg="green")
canvas.pack()
root.mainloop()
frame = tk.Frame(root, bg="white")
frame.place(relheight=0.8, relwidth=0.8)
I am using M1 MacOS if that information helps, here is a screenshot of the GUI, it is the same when I comment out the color customization in the code.

Tkinter Dark Theme by Default with Python 3.10?

I've just updated Python to 3.10 and when I run Tkinter programs they appear with a dark theme which I've never seen before. I'd like to go back to the standard light theme but I can't figure out how to. There doesn't seem to be any obvious documentation online regarding this.
Here is some quick sample code that just displays a grid to reproduce the dark theme (I'm using Python 3.10 and Visual Studio Code 1.61.2):
from tkinter import *
root = Tk()
class Something:
def __init__(self, parent, col, row):
canvas = Canvas(parent, bd=1, relief=SOLID, highlightthickness=0, width=30, height=30)
canvas.grid(column=col, row=row)
frame = Frame(root, bd=1, relief=SOLID)
frame.grid(padx=50, pady=50)
for i in range(11):
for j in range(11):
Something(frame, i, j)
root.mainloop()
There might be a problem with your default system UI.
I don't have a MAC so I can't test this, but try changing your default system ui to light, if MAC even has that option...

tkinter fill=Y does not work with Button [duplicate]

I am familiarizing myself with Tkinter, and I am attempting to write a very simple program, which displays a button in a window, using the pack geometry manager.
I was experimenting with various configuration options for pack(), such as expand, fill, and side, and I've run into a peculiar problem. I have written the following code:
from Tkinter import *
root = Tk()
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()
The problem is that the button expands to fill the window in the horizontal direction, but not the vertical direction. This is the same result that I get if instead of specifying fill=BOTH I use fill=X. In addition, if I specify instead fill=Y the button does not expand in either direction. Something seems to be going wrong with the fill in the vertical direction, and I cannot figure out what it might be.
I attempted to Google this problem and surprisingly found no mention of this happening to anyone else. I am using a Mac with OS X Yosemite and running python 2.7.5. I also attempted to compile with python 3.4.1 and saw no change.
Edit:
Based off of the answer and comments below, it is clear that there is nothing wrong with my code, because it seems to work on other machines. If not an error in the code, does anyone know what could possibly be causing the button to not stretch vertically when I run the above code?
This is a feature of native buttons on OSX. Buttons on OSX will be a fixed height and will not expand vertically. There is nothing you can do, short of using a different widget such as a label.
try running this code to see the behavior of fill and expand
from Tkinter import *
root = Tk()
root.geometry("500x500")
widget = Button(root, text='text1')
widget.pack(fill=X, expand=1)
widget = Button(root, text='text2')
widget.pack(fill=Y, expand=1)
widget = Button(root, text='text3')
widget.pack(fill=BOTH, expand=1)
root.mainloop()
Argument fill does fill in vertical direction as well
I am also beginner, defining geometry for fill was missing in your code as given below:
from Tkinter import *
root = Tk()
root.geometry("600x400")
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()

Tkinter pyimage doesn't exist

I know there are lot of similar questions, but there aren't any simple enough that I am able to understand. I have the following code:
import Tkinter as tk
from PIL import Image, ImageTk
class MainWindow:
def __init__(self, master):
canvas = Canvas(master)
canvas.pack()
self.pimage = Image.open(filename)
self.cimage = ImageTk.PhotoImage(self.pimage)
self.image = canvas.create_image(0,0,image=self.cimage)
filename = full_filename
root = tk.Tk()
x = MainWindow(root)
mainloop()
and I get the following error:
TclError: image "pyimage36" doesn't exist
I've read some stuff about the image objects getting garbage cleaned but I don't quite understand it.
Figured it out. For some reason, while running in the debugger, if any previous executions had thrown errors I get the "pyimage doesn't exist" error. However, if I restart the debugger (or no previously executed scripts have thrown errors), then the program runs fine.
I had the same error message when using spyder 3.3.6 the only way i could get the .png file to load and display after getting the 'Tinker pyimage error ' was to go to the Console and restart the kernel. After that i worked fine.
(Python 3.8)
If you are using a IDE with a console(such as Spyder) just call root.mainloop() in the console.
Odds are that you have a bunch of partially loaded tkinter GUI's that never managed to be executed due to an error that prevented the root.mainloop() function from being run.
Once you have run the root.mainloop() a bunch of GUI's will likely appear on screen. After you have closed all those GUI's try running your code again.
Image of multiple Tkinter GUI's appearing on screen
Read more about mainloop() here: https://pythonguides.com/python-tkinter-mainloop/
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
img = Image.open('Paste the directory path')
bg = ImageTk.PhotoImage(img)
lbl = Label(root, image=bg)
lbl.place(x=0, y=0)
mainloop()
I was getting the same error. Try this code this will help you.
Additionally, in case if you create a button and use it to open other window, then there use window = Toplevel(), otherwise it will again show the same error.
From programmersought
image “pyimage1” doesn’t exist
Because there can only be one root window in a program, that is, only one Tk() can exist, other windows can only exist in the form of a top-level window (Toplevel()).
Original code
import tkinter as tk
window = tk.TK()
Revised code
import tkinter as tk
window = tk.Toplevel()
Keep other code unchanged
https://www.programmersought.com/article/87961175215/

Creating a User Interface Using Tkinter (Python)

I looked through a tutorial on using Tkinter and saw that the following code:
>>> from Tkinter import *
>>> win=Tk()
This should produce a box with the title Tk and nothing else. However, when I try this code out no such box appears. I'm not getting any errors so I suspect it's working as intended. Is it possible that there are additional steps I have to take if I'm on a mac?
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
This code runs automatically, however, in the guide it suggests that I use $ python hello1.py to run this code, which doesn't work. Any ideas on why this might be?
However, this larger block does not work:
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(
frame, text="QUIT", fg="red", command=frame.quit
)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
root.destroy() # optional; see description below
The issue seems to have something to do with mainloop but I'm confused because at the same time that earlier block worked just fine with a root.mainloop() part.
Do you run this code in IDLE?
Try above code in terminal (not in IDLE), then it will work as expected.
So if you want to try and run it in Terminal you should follow the steps below
note- 'I find that running programs is Terminal that involve a tkinter Gui will often crash for me, however it may work for you'
1st - Open Terminal
2nd - Type 'python3.4' then press space bar once
3rd - Open a Finder window
4th - Go to where you saved your python file in the Finder window
5th - Once you have located the file in Finder, drag the file into the Terminal window
6th - Press enter, and enjoy your python program.
another note - 'It sounds like you need a better Python IDE, you should try out PyCharm it is a great Python IDE which you can code and run python programs in including tkinter stuff'
You can download PyCharm here https://www.jetbrains.com/pycharm/

Categories