how increase font size in text widget? - python

When I increase the size of the font using following code, it also increase the size of the widget. Is it possitlbe to increase font size by keeping the size of the text widget constant?
Thank You
A11 = tkinter.Text(top, height=28, width=70,background = "#02e0a1")
labelfont = ('times', 20, 'bold')
A11.config(font = labelfont)

If you force the GUI window to be a specific size, then changing the font of a text widget won't cause the text widget to grow*. It usually helps to set the width and height of the text widget to 1 (one) so that it won't even try to grow when you change the font.
well, the widget will try to grow, but the size constraint on the window will prevent the widget from getting too big.
Here's a simple contrived example.
import tkinter as tk
import tkinter.font as tkFont
class Example(object):
def __init__(self):
root = tk.Tk()
self.font = tkFont.Font(family="helvetica", size=18)
text = tk.Text(root, width=1, height=1, font=self.font)
button = tk.Button(root, text="Bigger", command=self.bigger)
button.pack(side="top")
text.pack(side="top", fill="both", expand=True)
text.insert("end", "Hello, world!")
# force the widow to a specific size after it's created
# so that it won't change size when we change the font
root.geometry("800x400")
def start(self):
tk.mainloop()
def bigger(self):
size = int(self.font.cget("size"))
size += 2
self.font.configure(size=size)
app = Example()
app.start()
The same technique can work on a smaller scale by putting the size constraint on a frame rather than the root window. If you put the text widget inside a frame, turn geometry propagation off, and then give the frame a fixed size, the widget will not grow. This is one of the few times when turning geometry propagation off can be useful.
Here's a modification of the above example, using this technique:
import tkinter as tk
import tkinter.font as tkFont
class Example(object):
def __init__(self):
root = tk.Tk()
self.font = tkFont.Font(family="helvetica", size=18)
button = tk.Button(root, text="Bigger", command=self.bigger)
# create a frame for the text widget, and let it control the
# size by turning geometry propagation off
text_frame = tk.Frame(root, width=800, height=400)
text_frame.pack_propagate(False)
text = tk.Text(text_frame, width=1, height=1, font=self.font)
text.pack(side="top", fill="both", expand=True)
button.pack(side="top")
text_frame.pack(side="top", fill="both", expand=True)
text.insert("end", "Hello, world!")
def start(self):
tk.mainloop()
def bigger(self):
size = int(self.font.cget("size"))
size += 2
self.font.configure(size=size)
app = Example()
app.start()

Widget size is determined by font size, so width=10 for a small font is smaller than width=10 for a large font. The following code only changes the font size.
import sys
if sys.version_info[0] < 3:
import Tkinter as tk ## Python 2.x
else:
import tkinter as tk ## Python 3.x
class DifferentFonts():
def __init__(self):
self.top=tk.Tk()
tk.Label(self.top, text="Small Font", width=10, bg="lightblue",
font=('DejaVuSansMono', 10)).grid(row=1)
tk.Label(self.top, text="Large Font", width=10, bg="lightyellow",
font=('DejaVuSansMono', 30)).grid(row=2)
tk.Button(self.top, text="Quit", bg="orange",
command=self.top.quit).grid(row=20)
self.top.mainloop()
DifferentFonts()

Related

Updating a label text with the click of a button in tkinter python

I am trying to get a label to display a random number when a button is clicked. I have tried get(), set() and config() in the function, as per similar cases on stackoverflow but to no avail. Where do I go wrong?
import tkinter as tk
import random
HEIGHT=200 #Window height
WIDTH=300 #Window width
TITLE="Random number" #Window title
LWIDTH=40 #Label width
LHEIGHT=50 #Label height
LFONTSIZE=44 #Label font size
def buttonpress():
LTEXT.set(random.randint(0, 100)) #The intention is for a new value to be calculated
l.text=LTEXT #The intention is for the label text to be updated. Have tried set(), get(), config()
BUTTONWIDTH=17 #button width
BUTTONHEIGHT=2 #button height, but in rows instead of pixels (!!!)
root=tk.Tk()
root.title(TITLE)
LTEXT=tk.IntVar(root) #defining the intvar
LTEXT.set(random.randint(0, 100)) #setting the initial value
f = tk.Frame(root,width=WIDTH,height=HEIGHT)
f.pack()
l=tk.Label(width=LWIDTH, height=LHEIGHT, text=LTEXT.get(),font=(None,LFONTSIZE))
l.place(relx=0.5, rely=0.3, anchor="center")
b=tk.Button(root,width=BUTTONWIDTH, height= BUTTONHEIGHT, text = "New number",command=buttonpress())
b.place(relx=0.5, rely=0.7, anchor="center")
root.mainloop()
To get the text form your label : print(mylabel["text"])
So to modify it : mylabel["text"] = myrandomnumber
It works with every parameter, for everything, buttons, labels, canvas, etc...
Exemple :
from tkinter import *
root = Tk()
label = Label(text="hello")
def change():
label["text"] = "world"
button = Button(text="Change", command=change)
label.pack()
button.pack()

TKInter Entry is not exapanding along with Main Window

I am using the following code, I am having 2 issues,
1. When I try to maximize the window, the Entry widget is not aligned/maximized along with main window and see a gap between scrolled text and Entry widget.
2. Second I am trying to set my cursor in Entry widget when I try to open or whenever my app is active but it's not working for some reason. Any Idea what am I making mistakes?
import tkinter as tk
from tkinter import scrolledtext
class Main:
def __init__(self, master):
self.master = master
master.title("Main")
width = master.winfo_screenwidth()
height = master.winfo_screenheight()
master.minsize(width=1066, height=766)
master.maxsize(width=width, height=height)
self.frame = tk.Frame(self.master)
text_area = scrolledtext.ScrolledText(self.master,width=75,height=35)
text_area.pack(side="top",fill='both',expand=True)
text_entry = tk.Entry(self.master,width=65)
text_entry.pack(side="top",fill=X, expand=True,ipady=3, ipadx=3)
text_entry.configure(foreground="blue",font=('Arial', 10, 'bold', 'italic'))
text_entry.focus()
self.frame.pack()
def initial(self):
print ("initializing")
def main():
root = tk.Tk()
app = Main(root)
root.mainloop()
if __name__ == '__main__':
main()
I can address the issue of your entry field not expanding properly.
That is because you have fill=X and this is not a valid input for fill. Instead use fill="x". I believe your 2nd issue with the entry field having a large gap is because you have set expand = True instead change that to expand = False.
That said I prefer to use the grid() geometry manager instead. Take a look at my below example of how to do this with grid and weights.
When using the grid() manager you can tell each widget exactly where you want it along a grid. The use of weights is for telling a row or column how much if any it should expand with the window. This combined with sticky="nsew" will help us control stuff expands within the window.
import tkinter as tk
from tkinter import scrolledtext
class Main(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("Main")
width = self.winfo_screenwidth()
height = self.winfo_screenheight()
self.minsize(width=1066, height=766)
self.maxsize(width=width, height=height)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=0)
self.columnconfigure(0, weight=1)
text_area = scrolledtext.ScrolledText(self,width=75,height=35)
text_area.grid(row=0, column=0, ipady=3, ipadx=3, sticky="nsew")
text_entry = tk.Entry(self,width=65)
text_entry.grid(row=1, column=0, ipady=3, ipadx=3, sticky="ew")
text_entry.configure(foreground="blue",font=('Arial', 10, 'bold', 'italic'))
text_entry.focus()
def initial(self):
print ("initializing")
if __name__ == '__main__':
root = Main()
root.mainloop()
Update:
To clarify on your issue with fill and expand I have updated your code with the correction so you can see it working.
import tkinter as tk
from tkinter import scrolledtext
class Main:
def __init__(self, master):
self.master = master
master.title("Main")
width = master.winfo_screenwidth()
height = master.winfo_screenheight()
master.minsize(width=1066, height=766)
master.maxsize(width=width, height=height)
self.frame = tk.Frame(self.master)
text_area = scrolledtext.ScrolledText(self.master,width=75,height=35)
text_area.pack(side="top",fill='both',expand=True)
text_entry = tk.Entry(self.master,width=65)
text_entry.pack(side="top",fill="x", expand=False, ipady=3, ipadx=3)
text_entry.configure(foreground="blue",font=('Arial', 10, 'bold', 'italic'))
text_entry.focus()
self.frame.pack()
def initial(self):
print ("initializing")
def main():
root = tk.Tk()
app = Main(root)
root.mainloop()
if __name__ == '__main__':
main()

Scrollable Frame Python Tkinter

I am working n a project that has a scroll able frame. It lets me add widgets to the frame but I can not get the frame to scroll and show the rest of the widgets. I have compared my code to other scroll able frames online and I could not notice the difference. Any one see the solution.
Code:
from Tkinter import *
import ttk
import os
class GUI(Frame):
def __init__(self, parent):
Frame.__init__(self,parent)
self.pack(fill=BOTH, expand=YES)
def gameView(self):
self.mainFrame = Frame(self)
self.mainFrame.pack(side=TOP)
self.scroller = ttk.Scrollbar(self.mainFrame, orient=VERTICAL)
self.scroller.pack(side=RIGHT, fill=Y)
self.canvas = Canvas(self.mainFrame, bd=0)
self.canvas.pack(fill=BOTH, side=LEFT)
self.viewArea = Frame(self.canvas, bg="Pink")
self.viewArea.pack(side=TOP, fill=BOTH)
self.canvas.config(yscrollcommand=self.scroller.set)
self.scroller.config(command=self.canvas.yview)
self.canvas.create_window((0,0), window=self.viewArea, anchor=NW, width=783, height=650)
self.viewArea.bind("<Configure>", self.scrollCom)
self.itemHolder = Frame(self.viewArea, bg="Pink")
self.itemHolder.pack(side=TOP)
self.gameGather()
def scrollCom(self, event):
self.canvas.config(scrollregion=self.canvas.bbox("all"), width=783, height=650)
def gameGather(self):
for i in range(0, 50):
label = Label(self.viewArea, text="Pie")
label.pack(side=TOP)
root = Tk()
root.title("School Vortex 2.0")
root.geometry("800x650")
root.resizable(0,0)
gui = GUI(root)
gui.gameView()
root.mainloop()
When you put the window on the canvas you are explicitly giving it a height and a width. Because of that, the actual width and height of the frame is completely ignored. Because the frame is almost exactly the height of the canvas, there's nothing to scroll.
If you remove the width and height options from the call to create_window your frame will be scrollable.

tkinter canvas image is not displayed in class

I am trying to display an image in python using the tkinter canvas option. However, if I input it in a class, like below, it doesn't give an error but also doesn't show my image. The buttons are displayed correctly though. Also, if I take the code for generating this image out of the class it works correctly. I can't seem to find out what the problem is.
import Tkinter as tk
from Tkinter import *
class Board(tk.Frame):
def __init__(self,parent):
frame = Frame(parent)
frame.pack()
tk.Frame.__init__(self,parent)
frame2 = Frame(frame)
frame2.pack()
c=Canvas(frame2)
c.pack(expand=YES,fill=BOTH)
background=PhotoImage(file='Board.gif')
c.create_image(100,100,image=background,anchor='nw')
button = Button(frame, text="Next turn", command=self.next_turn)
button.pack()
button = Button(frame, text="Roll the dice", command=self.roll)
button.pack()
....
root = Tk()
board = Board(root)
board.pack()
root.mainloop()
You have to keep a reference to the PhotoImage. This is just and example (you can also use self.background instead of c.background):
c = Canvas(frame2)
c.pack(expand=YES,fill=BOTH)
c.background = PhotoImage(file='Board.gif')
c.create_image(100,100,image=c.background,anchor='nw')

creating digital time in canvas tkinter

I am newbie in Python using tkinter and I have a problem that I cant solve.Digital time
I want to put digital time in the the upper right corner of my application (Please see the picture). I tried to search on net on how to create a digital time but it is on global root and frame configuration and I cant find a digital clock made for canvas. I also want to put my buttons in middle using grid, but I have no luck finding a solution. Can any one please help me? Ill paste my code here.
from tkinter import *
from tkinter import ttk
from datetime import date
import time
import sys
class main_menu(object):
def __init__(self, root):
self.root = root
self.root.title('System')
self.root.geometry('780x488')
self.background = PhotoImage(file='images/bg.png')
self.canvas = Canvas (root)
self.canvas.grid(sticky=N+S+W+E)
self.canvas.create_image(0,0, image=self.background, anchor="nw")
self.scan_photo = PhotoImage (file='images/scan.png')
self.logs_photo = PhotoImage (file='images/logs.png')
self.settings_photo = PhotoImage (file='images/settings.png')
self.scan_btn = Button (self.canvas, image=self.scan_photo, borderwidth=0, command=self.StartScan)
self.scan_win = self.canvas.create_window(225, 100, anchor="nw", window=self.scan_btn)
self.logs_btn = Button (self.canvas, image=self.logs_photo, borderwidth=0, command=self.Logs)
self.logs_win = self.canvas.create_window(225, 200, anchor="nw", window=self.logs_btn)
self.settings_btn = Button (self.canvas, image=self.settings_photo, borderwidth=0, command=self.Settings)
self.settings_win = self.canvas.create_window(225, 300, anchor="nw", window=self.settings_btn)
self.today = date.today()
self.format = self.today.strftime("%b. %d, %Y")
self.canvas.create_text(730, 30, text=self.format, font=("Helvetica", 10))
self.InstructionsLabel = Label(root, text="""
tadahhhhhh""", fg="black", font=("Calibri", 14))
self.Return_photo = PhotoImage (file='images/back_24x24.png')
self.ReturnMenu_btn = Button (self.canvas, image=self.Return_photo, background='white',activebackground='white', borderwidth=0, command=self.MainMenu)
self.ReturnMenu_win = self.canvas.create_window(0, 0, anchor="nw", window=self.ReturnMenu_btn)
###self.ReturnMenu = Button(root, image=self.back_photo, command=self.MainMenu, )
self.MainMenu()
def MainMenu(self):
self.RemoveAll()
self.ReturnMenu_btn.grid_remove()
self.scan_btn.grid(padx=215)
self.logs_btn.grid(padx=215)
self.settings_btn.grid(padx=215)
def StartScan(self):
self.RemoveAll()
def Logs(self):
self.RemoveAll()
self.ReturnMenu.grid()
def Settings(self):
self.RemoveAll()
self.ReturnMenu.grid()
def RemoveAll(self):
self.scan_btn.grid_remove()
self.logs_btn.grid_remove()
self.settings_btn.grid_remove()
self.InstructionsLabel.grid_remove()
self.ReturnMenu_btn.grid_remove()
if __name__ == '__main__':
root = Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
main_menu = main_menu(root)
root.mainloop()
To place the time in the upper right corner you need to know the width of the canvas. So use canvas.winfo_width() to get the width of the canvas and subtract some number to place it at the desired position.
If you want the time to stay at the top-right even if the window is resized then bind Configure to a function and move the text using .coords or .moveto.
Sample code(this code will make sure that the time is always at the upper right corner).
from tkinter import font
class MainMenu:
def __init__(self, root):
...
self.time = self.canvas.create_text(0, 0, text=self.format, font=("Helvetica", 10))
self.canvas.bind('<Configure>', self.adjustTimePosition)
...
def adjustTimePosition(self, event):
family, size = self.canvas.itemcget(self.time, 'font').split() # get the font-family and font size
text = self.canvas.itemcget(self.time, 'text')
txt_font = font.Font(family=family, size=size)
width, height = txt_font.measure(text), txt_font.metrics("ascent") # measures the width and height of the text
self.canvas.coords(self.time, self.canvas.winfo_width()-width, height) # moves the text

Categories

Resources