Background Image Adjustment on Top Level Window with Tkinter - python-3.x

I have a basic GUI that begins with a main menu of sorts. I have successfully set a background image to that menu and it also scales when I change the dimension of the GUI window.
However when I try to define some top-level windows that are opened by the sub-menu items the background image does not appear (not to mention scale).
Not sure what I'm doing wrong but I'm attaching the code I wrote along with the images of the basic GUI.
from tkinter import *
from tkinter import ttk, font, messagebox
from PIL import ImageTk, Image
import os
root = Tk()
root.title("Decoder of ultrasound images to detect colon tumors")
# Adding window icon
root.iconbitmap('afekaImage.ico')
rootWidth, rootHeight = 600, 600
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
topLeftPosition = (screenWidth / 2 - rootWidth / 2, screenHeight / 2 - rootHeight / 2)
# Configure window size
root.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')
'''
# Create username & password entry
def entryDialog():
userName = entry1.get()
password = entry2.get()
if ((userName == 'Itzhak.Mamistvalov' and password == '311396832') or
(userName == 'AssafHasky' and password == '308333533')):
messagebox.showinfo('info', 'Correct Login')
else:
messagebox.showinfo('info', 'Invalid Login') '''
# open doc file
def openDocFile():
os.startfile("mid_sub.docx")
# adjusting background image to fit window
def adjustBackgroundImage(event):
# avoid garbage collection option 1
# global resizedBackgroundImage, newBackgroundImage
# ----------
width = event.width
height = event.height
resizedBackgroundImage = copyImage.resize((width, height))
newBackgroundImage = ImageTk.PhotoImage(resizedBackgroundImage)
label.config(image=newBackgroundImage)
# avoid garbage collection option 2
label.image = newBackgroundImage
# ----------
def createUserManualWindow(button_userManual):
global image1
userManualWindow = Toplevel(root)
def activateButtonUserManual():
button_userManual.configure(state="normal")
userManualWindow.destroy()
button_userManual.configure(state="disabled")
button_exit_userManualWindow = Button(userManualWindow, text="Exit", font=fontStyle,
command=lambda: [userManualWindow.destroy(), activateButtonUserManual()])
button_exit_userManualWindow.place(relx=0.4, rely=0.8, relwidth=0.2, relheight=0.1)
# will occurs only when esc pressed
userManualWindow.protocol("WM_DELETE_WINDOW", activateButtonUserManual)
# ----------
userManualWindow.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')
userManualWindow.iconbitmap('afekaImage.ico')
image1 = ImageTk.PhotoImage(Image.open('background.jpg'))
label1 = ttk.Label(userManualWindow, image=image1).pack()
label1.bind('<Configure>', adjustBackgroundImage)
label1.pack(fill=BOTH, expand=YES)
def createOverviewWindow(button_userManual):
overviewWindow = Toplevel(root)
def activateButtonOverview():
button_userManual.configure(state="normal")
overviewWindow.destroy()
button_userManual.configure(state="disabled")
button_exit_OverviewWindow = Button(overviewWindow, text="Exit", font=fontStyle,
command=lambda: [overviewWindow.destroy(), activateButtonOverview()])
button_exit_OverviewWindow.place(relx=0.4, rely=0.8, relwidth=0.2, relheight=0.1)
# will occurs only when esc pressed
overviewWindow.protocol("WM_DELETE_WINDOW", activateButtonOverview)
# ----------
overviewWindow.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')
overviewWindow.iconbitmap('afekaImage.ico')
# Define background image
image = Image.open('background.jpg')
copyImage = image.copy()
backgroundImage = ImageTk.PhotoImage(image)
label = ttk.Label(root, image=backgroundImage)
label.bind('<Configure>', adjustBackgroundImage)
label.pack(fill=BOTH, expand=YES)
# Configure font
fontStyle = font.Font(family="Segoe Script", size=10, weight=font.BOLD)
# Create buttons
button_userManual = Button(root, text="USER MANUAL", command=lambda: createUserManualWindow(button_userManual), font=fontStyle)
button_userManual.place(relx=0.4, rely=0.2, relwidth=0.2, relheight=0.1)
button_overview = Button(root, text="OVERVIEW", command=lambda: createOverviewWindow(button_overview), font=fontStyle)
button_overview.place(relx=0.4, rely=0.4, relwidth=0.2, relheight=0.1)
button_openDocFile = Button(root, text="DOC FILE", font=fontStyle, command=openDocFile)
button_openDocFile.place(relx=0.4, rely=0.6, relwidth=0.2, relheight=0.1)
button_quit = Button(root, text="Exit", font=fontStyle, command=root.destroy)
button_quit.place(relx=0.4, rely=0.8, relwidth=0.2, relheight=0.1)
root.mainloop()

label1 = ttk.Label(userManualWindow, image=image1).pack() should be changed to:
label1 = ttk.Label(userManualWindow, image=image1)
label1.pack()
You should call label1.pack() before placing the "Exit" button, otherwise it will overlap/hide the "Exit" button. Or call label1.lower() after label1.pack().
label is used inside adjustBackgroundImage(), so even though you bind <configure> on label1 to adjustBackgroundImage(), it would not resize image shown by label1. Use event.widget instead of label inside adjustBackgroundImage():
def adjustBackgroundImage(event):
label = event.widget
# avoid garbage collection option 1
# global resizedBackgroundImage, newBackgroundImage
# ----------
width = event.width
height = event.height
resizedBackgroundImage = copyImage.resize((width, height))
newBackgroundImage = ImageTk.PhotoImage(resizedBackgroundImage)
label.config(image=newBackgroundImage)
# avoid garbage collection option 2
label.image = newBackgroundImage
# ----------

Related

Drawing shapes in place of using image

So I have a class that displays some text with a background and an image of a shape that goes along with it. I want to create/draw the shapes inside this class instead of using an actual image file. I found some code that draws a circle/diamond but I have been having trouble implementing it inside my class. What would be some ways to do this?
This is my class:
from tkinter import Tk, Button, Label, BOTTOM, CENTER
from PIL import ImageTk, Image
# set properties of window
window_width = 1920
window_height = 238
# set properties of image
# NOTE: pixel dims of image file mapped 1 to 1 on window
image_width = 238
image_height = 238
# set properties of label
label_width = window_width - image_width
label_height = 238
background_color = 'navy'
foreground_color = 'yellow'
# set properties of font
font_family = 'Helvetica'
font_height = 110
font_weight = 'bold'
# variables for text switching
testIndex = 0
text = [
'Test Test Test',
'TEST TEST !!!',
'12345678901234567890'
]
class GUIClass:
def __init__(self, root):
# bringing text variables into class scope
self.root = root
self.testIndex = testIndex
self.text = text
# creating image for sign
self.image = ImageTk.PhotoImage(Image.open("testImage.png"))
self.imageLabel = Label(image=self.image, width=image_width, height=image_height, bg=background_color)
self.imageLabel.place(x=0, y=0, width=image_width, height=image_height)
# creating label, setting font, position, and binding left-click event listener
self.label = Label(root, text=text[testIndex], bg=background_color, fg=foreground_color, borderwidth=10, relief="solid")
self.label['font'] = (font_family, font_height, font_weight)
self.label.bind("<Button-1>", self.closeScreen)
self.label.place(x=image_width, y=0, width=label_width, height=window_height)
# creating close button to close window (necessary if removing border of window)
#self.close_button = Button(root, text='close', command= root.quit)
#self.close_button.pack(side=BOTTOM)
#set timer to cycle through strings
self.root.after(1500, self.timerTest)
# threaded event handler for when timer elapses
def timerTest(self):
self.testIndex = self.testIndex + 1
self.testIndex %= len(self.text)
self.label['text'] = self.text[self.testIndex]
self.root.after(1500, self.timerTest)
# closes window when label is left-clicked
def closeScreen(self, event):
self.root.quit()
# creates the window
root = Tk()
# sets dimensions of window (ex: "200x200")
root.geometry(str(window_width) + "x" + str(window_height))
# removes boundary, disables resizing, and removes buttons of window
root.overrideredirect(True)
# attaches the window to the GUI class
rootGUI = GUIClass(root)
# continously loops, draws display, and waits for user input
root.mainloop()
This is the code I found for the circle/diamond shape:
from tkinter import Tk, Canvas, Frame, BOTH
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Shapes")
self.pack(fill=BOTH, expand=1)
canvas = Canvas(self)
canvas.create_oval(5, 5, 233, 233, outline="#fb0", fill="#fb0", width=0)
points = [119, 5, 233, 119, 119, 233, 5, 119]
canvas.create_polygon(points, outline='#fb0', fill='#fb0', width=0)
canvas.pack(fill=BOTH, expand=1)
def main():
root = Tk()
ex = Example()
root.geometry("330x220+300+300")
root.mainloop()
if __name__ == '__main__':
main()

Skip files on button click [tkinter + Python 3.5]

Suppose I have 'n' image files in a directory filedir which I want to view via tkinter console. Now, I want to skip a few files whenever necessary, using a button click event which invokes a function nextFile().
E.g.
import os
def nextFile():
global img
img_2 = process(img)
return img_2
window = tk.Tk()
window.title("File Viewer")
files = os.listdir(filedir)
button1 = tk.Button(window, text="Browse Files...", fg="black", command=askopenfilename)
button2 = tk.Button(window, text="SELECT", width=50, command=nextFile)
canvas = tk.Canvas(window, width=width, height=height)
button1.pack(side=tk.LEFT)
button2.pack(side=tk.BOTTOM)
canvas.pack()
for f in files:
img = cv2.cvtColor(cv2.imread(filedir + '/' + f), cv2.COLOR_BGR2RGB)
photo = ImageTk.PhotoImage(image=Image.fromarray((img))
canvas.create_image(0, 0, image=photo, anchor=tk.CENTER)
window.mainloop()
Any help is appreciated. Thanks!
Here is a simple example using PIL to load the inital image and then use a button and function to load each next image.
import tkinter as tk
from PIL import ImageTk, Image
import os
path = 'C:/your/file/path/here'
def nextFile():
# the global is needed to keep track of current index and also keep
# a reference of the image being displayed so its is not garbage collected.
global current_ndex, image
current_ndex += 1
if current_ndex < len(files):
img = Image.open('{}/{}'.format(path, files[current_ndex]))
image = ImageTk.PhotoImage(img)
# clear the canvas before adding the new image.
canvas.delete("all")
canvas.create_image(0, 0, image=image, anchor=tk.CENTER)
my_window = tk.Tk()
my_window.title("File Viewer")
files = os.listdir(path)
current_ndex = 0
button2 = tk.Button(my_window, text="Next", width=50, command=nextFile)
canvas = tk.Canvas(my_window, width=100, height=100)
button2.pack(side=tk.BOTTOM)
canvas.pack()
first_image = files[0]
img = Image.open('{}/{}'.format(path, first_image))
image = ImageTk.PhotoImage(img)
canvas.create_image(0, 0, image=image, anchor=tk.CENTER)
my_window.mainloop()

How to activate tkinter window after destroying the previously one?

I need the next tkinter window to become active automatically after destroying the previous one.
I'm working with Python 3 in Windows 10.
import sys
from tkinter import *
#Green button pressed
def passed():
fails='0'
with open("ButtonPressed.txt",'w')as TestResult:
TestResult.write(fails)
TestResult.close()
root.destroy()
#Red button pressed
def failed():
fails='1'
with open("ButtonPressed.txt",'w')as TestResult:
TestResult.write(fails)
TestResult.close()
root.destroy()
#First window
def PushHold(UUT):
global root
root = Tk()
root.bind('<Return>', lambda q:passed())
TitleText="Push Button test"
root.title(TitleText)
root.geometry("460x160")
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
LABLE_TEXT = "Push and hold S1 on "+ UUT+" board.\n Click OK."
label = Label( frame, text=LABLE_TEXT)
label.config(font=("Arial", 22))
label.pack( side = TOP)
greenbutton = Button(frame, text="OK", bg="Green", fg="White", height=2 , width = 10 , command=lambda: passed())
greenbutton.config(font=("Arial", 18))
greenbutton.pack( side = LEFT, padx=140)
root.mainloop()
#Second window
def Release(UUT):
global root
root = Tk()
root.bind('<Return>', lambda q:passed())
TitleText="Release Button test"
root.title(TitleText)
root.geometry("460x160")
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
LABLE_TEXT = "Release S1 on "+ UUT+" board.\n Click OK."
label = Label( frame, text=LABLE_TEXT)
label.config(font=("Arial", 22))
label.pack( side = TOP)
greenbutton = Button(frame, text="OK", bg="Green", fg="White", height=2 , width = 10 , command=lambda: passed())
greenbutton.config(font=("Arial", 18))
greenbutton.pack( side = LEFT, padx=140)
root.mainloop()
#read results of the first window
def PushButton(UUT):
PushHold(UUT)
with open("ButtonPressed.txt",'r') as TestResult:
str = list(TestResult)
fails = int(str[0])
TestResult.close()
return fails
#read results of the second window
def ReleaseButton(UUT):
Release(UUT)
with open("ButtonPressed.txt",'r') as TestResult:
str = list(TestResult)
fails = int(str[0])
TestResult.close()
return fails
PushButton('UUT1') #first window calling
ReleaseButton('UUT1') #second window calling
The first window is working good, the second window appears, but not active. It is necessary to click on it to activate before pressing the button.
Trials to use root.withdraw() or root.lift() did not succeed.
On my system the second window takes focus without altering your code, so it is hard to tell. You could probably use root.focus_force() prior to calling root.mainloop()
Alternatively, you could spawn different windows from the same root, using tk.Toplevel.
import sys
from tkinter import *
#Green button pressed
def passed():
fails='0'
with open("ButtonPressed.txt",'w')as TestResult:
TestResult.write(fails)
TestResult.close()
root.destroy()
#Red button pressed
def failed():
fails='1'
with open("ButtonPressed.txt",'w')as TestResult:
TestResult.write(fails)
TestResult.close()
root.destroy()
#First window
def PushHold(UUT):
global root
root = Tk()
root.bind('<Return>', lambda q:passed())
TitleText="Push Button test"
root.title(TitleText)
root.geometry("460x160")
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
LABLE_TEXT = "Push and hold S1 on "+ UUT+" board.\n Click OK."
label = Label( frame, text=LABLE_TEXT)
label.config(font=("Arial", 22))
label.pack( side = TOP)
greenbutton = Button(frame, text="OK", bg="Green", fg="White", height=2 , width = 10 , command=lambda: passed())
greenbutton.config(font=("Arial", 18))
greenbutton.pack( side = LEFT, padx=140)
root.focus_force() #<-------------- Here ---------------------
root.mainloop()
#Second window
def Release(UUT):
global root
root = Tk()
root.bind('<Return>', lambda q:passed())
TitleText="Release Button test"
root.title(TitleText)
root.geometry("460x160")
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
LABLE_TEXT = "Release S1 on "+ UUT+" board.\n Click OK."
label = Label( frame, text=LABLE_TEXT)
label.config(font=("Arial", 22))
label.pack( side = TOP)
greenbutton = Button(frame, text="OK", bg="Green", fg="White", height=2 , width = 10 , command=lambda: passed())
greenbutton.config(font=("Arial", 18))
greenbutton.pack( side = LEFT, padx=140)
root.focus_force() #<-------------- Here ---------------------
root.mainloop()
#read results of the first window
def PushButton(UUT):
PushHold(UUT)
with open("ButtonPressed.txt",'r') as TestResult:
str = list(TestResult)
fails = int(str[0])
TestResult.close()
return fails
#read results of the second window
def ReleaseButton(UUT):
Release(UUT)
with open("ButtonPressed.txt",'r') as TestResult:
str = list(TestResult)
fails = int(str[0])
TestResult.close()
return fails
PushButton('UUT1') #first window calling
ReleaseButton('UUT1') #second window calling

Tkinter background color issue at scrollbar

I am working on a tkinter script which has a vertical and horizontal scrollbar.
A portion of the window below the vertical scrollbar is not picking up the background color I'm applying.
I have tried the following color options still the small portion is not picking up.
Image:
Sample window snapshot
Full Code:
from tkinter.tix import *
from tkinter import *
import collections
root = Tk()
root.configure(background='steel blue')
# Global variables
fname = ''
# Variables for setting the height and width of widget
# Variables to set Height
actualRootHeight = 300
rootHScale = 25
rootHOffset = 100
canvasHeight = 300
root2CanvasHMargin =65
# Variables to set Width
rootWScale = 10
rootWOffset = 200
canvasWidth = 300
root2CanvasWMargin = 20
inpWidth = 0
# Lists to save configs
inpParamList = collections.OrderedDict()
paramListRef = collections.OrderedDict()
updatedParamList = collections.OrderedDict()
entryList = []
labels = []
# All widget coding is done here
class guiList(Frame):
global root
# Constructor - Use as a control structure
def __init__(self,parent):
Frame.__init__(self,parent)
self.parent = parent
self.readParams()
self.setGeometry()
self.initUI()
self.controlUI()
def onFrameConfigure(self, event, Canvas1):
# Reset the scroll region to encompass the inner frame
Canvas1.configure(scrollregion=Canvas1.bbox("all"), background='steel blue')
# All widget edition is done here
def initUI(self):
global paramListRef
titleStr = sys.argv[1]
self.parent.title(titleStr)
self.grid(row=0, column=0)
inpConfigs = inpParamList.items()
# Add a canvas and call Frame as it's child widget
# Scrollbar can be added to Canvas
Canvas1 = Canvas(self, width=canvasWidth, height=canvasHeight, borderwidth=0, bg="light steel blue")
vsb = Scrollbar(self, orient="vertical", command=Canvas1.yview, bg="light steel blue", troughcolor="steel blue", highlightcolor="light steel blue", activebackground="light steel blue", highlightbackground="light steel blue")
Canvas1.configure(yscrollcommand=vsb.set)
vsb.grid(column=2, sticky='NS')
hsb = Scrollbar(self, orient="horizontal", command=Canvas1.xview, bg="light steel blue", troughcolor="steel blue")
Canvas1.configure(xscrollcommand=hsb.set)
hsb.grid(column=0, sticky='EW')
Canvas1.grid(row=0, column=0, sticky='NWES')
# Create new Frame for input configs
Frame1 = Frame(Canvas1, width=canvasWidth, height=canvasHeight, bg="light steel blue")
Canvas1.create_window((1,1),window=Frame1, anchor="nw", tags="Frame1")
Frame1.bind("<Configure>", lambda event, arg=Canvas1: self.onFrameConfigure(event, arg))
# Loop through the input configs
i = 0
# Add label and combobox in loop
for k,v in inpConfigs:
# Label widgets
lbl1 = Label(Frame1, text=k, bg="light steel blue", font=("Helvetica", 12, "bold"), fg="steel blue")
lbl1.grid(row = i, column = 0, padx=10, pady=5, sticky='W')
labels.append(lbl1)
# Combo-box widget for configurations
tkvar = StringVar(Frame1)
tkvar.set(v[0])
entry1 = OptionMenu(Frame1, tkvar, *v)
entry1.configure(width=20, anchor=W, bg="steel blue", fg="white", font=("Helvetica", 11, "bold"))
entry1.grid(row = i, column=1, padx=10, pady=5, sticky='E')
#entry1.grid_columnconfigure(2, weight=2)
paramListRef[k] = tkvar
i += 1
# Read the updated configs after the button click
def readUpdatedParams(self):
global updatedParamList
for k,v in paramListRef.items():
updatedParamList[k] = v.get()
root.destroy()
self.writeBack()
# Seperate Frame for buttons
# Upon clicking read updted params
def controlUI(self):
Frame2 = Frame(self, bg="steel blue")
Frame2.grid(row=2, column = 0, sticky="EW")
b = Button(Frame2, text="OK", command=self.readUpdatedParams, bg="light steel blue", fg="steel blue", font=("Helvetica", 11, "bold"))
#b.grid(row=1, column=1, pady=10)
b.pack(fill="none", expand=True, pady = 10)
# Read the file and create a key, value pair for configs
# Lines in file is split with space as delimiter
# First column is the config name and rest are all possible value
def readParams(self):
global inpParamList
global inpWidth
f = open(fname)
for line in f:
val = {}
val = line.split()
key = val.pop(0)
# Get the max width of key to adjust widget width
inpWidth = len(key) if (len(key) > inpWidth) else inpWidth
inpParamList[key] = val
# Geometry ( X-width x Y-width + X-position + Y-position)
# Based on the number of elements in the config list
# the height of the widget is set (Max is 75% of screen size)
def setGeometry(self):
global actualRootHeight
global canvasWidth
global canvasHeight
listLen = len(inpParamList)
rootWinwidth = int(inpWidth *rootWScale) + rootWOffset
rootWinheight = (listLen * rootHScale ) + rootHOffset
screenWidth = self.winfo_screenwidth()
screenHeight = int(self.winfo_screenheight() * 0.75)
if rootWinheight < screenHeight :
actualRootHeight = rootWinheight
else :
actualRootHeight = screenHeight
canvasWidth = rootWinwidth - root2CanvasWMargin
canvasHeight = actualRootHeight - root2CanvasHMargin
rootWinresolution = str(rootWinwidth)+'x'+str(actualRootHeight)+'+'+'0'+'+'+'0'
root.geometry(rootWinresolution)
# Sub routine to write back the config file.
def writeBack(self):
fr = open("bt_top.param.config",'w')
for k,v in updatedParamList.items():
print(k,v)
fr.write(k)
fr.write(" ")
fr.write(v)
fr.write("\n")
fr.close()
# Main Function
# Define Window geometry and other top level stuff here
# Do not go into widget coding here
def main():
global fname
# Get File name from command line argument
fname = sys.argv[2]
app = guiList(root)
root.mainloop()
# The __name__ variable decides what to run
# Below lines make this file run stand-alone
if __name__ == '__main__':
Any suggestions are welcome. Thanks
-Vinay
The piece of black is the background of self, your guiList class. Add a line self.config(bg="steel blue") to its __init__() function (or to initUI() I suppose) to fix it.

python 3.5 tkinter text box recording

I have created a program that to record the input in a text box. After that, it would print it in a text
Here is the code:
from tkinter import*
window = Tk()
window.geometry("500x300")
a = Label(window, text="hi", font=('Helvetica', 32))
a.place(x=70, y = 80)
entry_box2=Entry(window,)
entry_box2.place(x=110, y=100)
def retrieve_input():
input = entry_box2.get("1.0",'end-1c')
a = Label(window, text= input, font=('Helvetica', 32))
a.place(x=70, y = 80)
window.mainloop()
I hope I could get a response as fast as posiblle
You have to use retrieve_input. You can bind() it to Entry and execute when Enter is pressed.
entry_box2.bind('<Return>', retrieve_input)
And then you can add new Label or replace text in existing one.
import tkinter as tk
# --- functions ---
def retrieve_input(event):
# get text from Entry
#txt = ent.get()
txt = event.widget.get()
# create new label with text
l = tk.Label(root, text=txt)
l.pack()
# or change existing label
lab['text'] = txt
# clear entry
#ent.delete(0, len(txt))
event.widget.delete(0, len(txt))
# --- main ---
root = tk.Tk()
lab = tk.Label(root, text="Hello")
lab.pack()
ent = tk.Entry(root)
ent.pack()
ent.bind('<Return>', retrieve_input)
root.mainloop()

Resources