How to stop text in tkinter from appearing blurry/pixelated in Linux? - python

I am using tkinter in python. Sharing the image [1]: https://i.stack.imgur.com/54xjq.png. The fonts I am using are:
font=("Ariel", 48, "italic")
font=("Ariel", 64, "italic")

Related

Tkinter button background color is not working in mac os

I am trying to change the bg color of a tkinter button on my mac (catalina) but instead of getting a colored background, it is showing a blank space in the layout.
The button code I used:
OpeningFile = Button(root, width=45, bg="#82CC6C", fg="black", text="OPEN", highlightbackground="#82CC6C", highlightthickness=1, borderwidth=0.2, relief="groove", padx=0, pady=0, command=openfile)
OpeningFile.grid()
Result I am getting:
What I expected:
I tried changing many parameters but it is still giving me the same result,
Can it be fixed or it is a bug in tkinter inside mac only?
(It was working properly in windows)
I got the fix:
Use tkmacosx module for tkinter buttons in mac, use "from tkmacosx import Button", and then change the parameters and use borderless=1 to remove the unnecessary layout.
You can see the result I got after using the module:
I was looking for information on the same issue and it seems to be a limitation with tkinter using the MacOS APIs. Basically MacOS says the button colors can't change so tkinter can't change them.
https://github.com/python/cpython/issues/88409

Wingdings Font Not Shown in Tkinter on Macbook

Whenever I run the following snippet (on MacOs 10.13.1 using Python 3.6.1):
import tkinter
window = tkinter.Tk()
open_file = tkinter.Button(window, text="1", font=("Wingdings", 12))
open_file.pack()
Instead of showing the following glyph:
The program shows this:
Is there a way in which I can fix this so that the Wingdings font displays correctly?
(Note: The font shows up fine on Windows 10)

Use OptionMenu as a Menu in Python using Tkinter

To increase my understanding and usability of python I have been building a text editor from scratch. What I'm trying to do now is allow the user to change the size of the font. I have this working using an OptionMenu but by using that I have to have a drop down on the interface. What I'm trying to do is somehow put the option menu into a menu on the top bar (with file_menu = Menu(root)) and make it look like a cascade. I don't have my exact code as I'm on mobile and away from my laptop, but I've been thinking about how to do this and I can't figure it out And wasn't able to find it searching earlier. Thanks in advance!
EDIT: I figured out how to do it, and I apologize for not having my original code. My original code was something like the answer posted below me, but I wanted to let the user select font sizes ranging from 8-40 so having
font_size.add_command(label='8', command=lambda: font_size_changer(8))
font_size.add_command(label='10', command=lambda: font_size_changer(10))
font_size.add_command(label='12', command=lambda: font_size_changer(12))
Just looked bad for how many I wanted. I ended up not doing the OptionMenu and IntVar() and went with a for loop like so:
def font_size_changer(clicked_size):
global current_font_size
current_font_size = clicked_size
text.config(font=(current_font, current_font_size, style_combo))
sizes_list = [8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
font_size = Menu(menu)
font_menu.add_cascade(label="Font Size", menu=font_size)
for x in range(0, len(sizes_list)):
font_size.add_command(label=str(sizes_list[x]), command=lambda: font_size_changer(sizes_list[x]))
Whilst you could use an OptionMenu to do this Tkinter actually has a native Menu object which we could use to achieve similar results much more cleanly and with less code.
This is actually not too tricky to pull off, see below for an example:
import tkinter as tk
class App:
def __init__(self, root):
self.root = root
self.label = tk.Label(self.root, text="Lorem Ipsum", font=("Comic Sans MS", 44))
self.menubar = tk.Menu(self.root)
self.menu = tk.Menu(self.root, tearoff=0)
self.menu.add_command(label="Small", command=lambda:self.label.config(font=("Comic Sans MS", 22)))
self.menu.add_command(label="Medium", command=lambda:self.label.config(font=("Comic Sans MS", 44)))
self.menu.add_command(label="Big", command=lambda:self.label.config(font=("Comic Sans MS", 66)))
self.menubar.add_cascade(label="Size", menu=self.menu)
self.root.config(menu=self.menubar)
self.label.pack()
root = tk.Tk()
App(root)
root.mainloop()
So let's break this down.
We create two Menu objects menubar and menu. We then store all of the commands inside of menu, this creates a fleshed out Menu object with a few entries in them.
Each command is setup to change the font size of label
We then add a cascade item to menubar selecting the predefined menu as our Menu object for the cascade.
root.config(menu=self.menubar is then used to "draw" the Menu object on our window.
This results in a cascading menu at the top of the screen where each option in the menu allows us to change the font size of a label object.

How do I disable keyboard input into an Entry widget, disable resizing of tkinter window and hide the console window?

I am making a calculator using tkinter and I wish to do the following:
Disable keyboard input for the Entry widget so that the user can only input through the buttons.
Even after disabling keyboard input for the Entry widget, I wish to be able to change the background and foreground of the widget.
I wish to hide the console window because it is totally useless in the use of this calculator.
I don't want to let the user resize the root window. How do I disallow the resizing of the root window?
Here is my code so far...
from tkinter import *
root = Tk()
root.title("Calculator")
root.config(background="black")
operator = ""
textVar = StringVar()
def valInput(number):
global operator
operator+=str(number)
textVar.set(operator)
display = Entry(root, textvariable=textVar, font=("Arial", 14, "bold"), bg="lightblue", fg="black", justify="right")
display.grid(row=0, column=0, columnspan=4)
btn7 = Button(root, font=("Arial", 12, "bold"), bg="orange", fg="red", text="7", command= lambda : valInput(7))
btn7.grid(row=1, column=0)
"""
And more buttons...
"""
root.mainloop()
As you can see, I can input into the Entry widget using buttons but later on, after the calculator is complete, if the user inputs characters like abcd... it will cause problems and show errors. How do I disallow keyboard entry so that I can avoid these errors?
I want to make my calculator a bit colorful. I changed the color of the root window, the buttons and also the color of the Entry widget. Is there any way to change the color of the widget even after it is disabled?
I don't need the console window while using this calculator. How do I hide it?
If I resize the root window, the calculator becomes ugly, besides, resizing the window isn't necessary. So how do I prevent the user from resizing the window?
To be able to disable keyboard input in Entry(args)
Set the state to disabled:
display = Entry(root, state=DISABLED)
To be able to disable the feature of resizing the tkinter window (so that you can't drag and stretch it.
root.resizable(0,0)
To be able to make the command prompt window disappear. (I just want the tkinter window.
Rename the file with a .pyw extension (assuming you are using windows)
Don't use from tkinter import * it's really not recommended because it pollutes the main namespace with every public name in the module. At best this makes code less explicit, at worst, it can (and it will) cause name collisions.
Have the right reflexes, use import tkinter or import tkinter as tk instead
this should work, you have to use the disabledbackground option :
import tkinter as tk
root = tk.Tk()
display = tk.Entry(root,font=('Arial', 20, 'bold'), disabledbackground='lightblue', state='disabled')
display.pack()
root.resizable(0,0)
root.mainloop()

Python tkinter label image not drawn unless a message widget is behind it

I have a transparent tkinter message widget and a label, one containing text the other an image, the image is only drawn in the parts where the text label is behind it. 1
I'm using python tkinter on windows 7 to draw the labels on the desktop on a transparent window, so what you're seeing in the background is my regular windows desktop, which might have something to do with it
Here are snippets of code, but there is alot more that's not relevant I think
x = Tk()
label = tkinter.Message(x, textvariable=text, font=('Terminal','10'), fg='white', bg='green', width=800, anchor='n')
label.master.overrideredirect(True)
label.master.geometry('+30+30')
label.master.lift()
label.master.wm_attributes('-transparentcolor', 'green')
label.pack()
Then this is inside a function which is called to display the plot
new_plot = Label(x, image=plot_image, fg='white', bg='green', anchor='n', width=640, height=480)
new_plot.image = plot_image
new_plot.master.wm_attributes('-transparentcolor', 'green')
new_plot.place(x = 20, y = 30, width=640, height=480)
new_plot.update()
Then at the end
x.mainloop()
I hope this is all that's relevant to the problem
I suspect it has to do with the fact that a tkinter window by default is 1x1 pixel in size. When you use place, tkinter will not expand the window to fit its contents.
The reason it works with the messagebox is because you are adding the messagebox to the root window with pack which will cause the window to grow.
The simple solution is to use new_plot.pack(...) instead of new_plot.place(...).

Categories