How to put a background image in a 2nd Frame? - python

I need help. I need to bring out the image background to my new created frame in genkeymenu function. But the problem is, once the frame is created, the image background only seems to change in the first created frame. I tried to search for solutions but nothing works. Can I ask what's the problem?
import Tkinter as tk
from Tkinter import *
from PIL import Image, ImageTk
def genkeymenu():
generatemenu = tk.Toplevel(mainmenu)
bg1 = ImageTk.PhotoImage(file="key2.jpg")
background_label = Label(image=bg1)
background_label.place(x=0, y=0)
background_label.image = bg1
keynamelabel = Label(generatemenu, text="Enter your key name")
keynameEntry = Entry(generatemenu)
keynameButton = Button(generatemenu, text="Enter")
check1024= Checkbutton(generatemenu, text="1024 bit")
check2048= Checkbutton(generatemenu, text="2048 bit")
check4096= Checkbutton(generatemenu, text="4096 bit")
tk.background_label.grid(row=0)
keynamelabel.grid(row=0)
keynameEntry.grid(row=1)
keynameButton.grid(row=2)
check1024.grid(row=3, column=0)
check2048.grid(row=3, column=1)
check4096.grid(row=3, column=2)
generatemenu.title("Generate Key")
generatemenu.mainloop()
mainmenu = tk.Tk()
bg = ImageTk.PhotoImage(file="key.jpg")
background_label = Label(image=bg)
background_label.place(x=0, y=0)
genkeybutton = Button(mainmenu, text= "Generate Key Pair", fg="black", command=genkeymenu)
encryptbutton = Button(mainmenu, text= "Encrypt your message", fg="black")
decryptbutton = Button(mainmenu, text= "Decrypt your message", fg="black")
background_label.grid(row=0)
genkeybutton.grid(row=0, column=0, sticky = N, rowspan=2)
encryptbutton.grid(row=0, column=0)
decryptbutton.grid(row=0, column=0, sticky=S)
mainmenu.title("RSA ENCRYPTION")
mainmenu.mainloop()

You specify all the widget to appear in the toplevel window but didn't do that for the background. background_label = Label(generatemenu, image=bg1)
import Tkinter as tk
from Tkinter import *
PIL import Image, ImageTk
def genkeymenu():
generatemenu = tk.Toplevel(mainmenu)
bg1 = ImageTk.PhotoImage(file="key2.jpg")
background_label = Label(generatemenu, image=bg1)# specify the window you it to appear.
background_label.place(x=0, y=0)
background_label.image = bg1
keynamelabel = Label(generatemenu, text="Enter your key name")
keynameEntry = Entry(generatemenu)
keynameButton = Button(generatemenu, text="Enter")
check1024= Checkbutton(generatemenu, text="1024 bit")
check2048= Checkbutton(generatemenu, text="2048 bit")
check4096= Checkbutton(generatemenu, text="4096 bit")
tk.background_label.grid(row=0)
keynamelabel.grid(row=0)
keynameEntry.grid(row=1)
keynameButton.grid(row=2)
check1024.grid(row=3, column=0)
check2048.grid(row=3, column=1)
check4096.grid(row=3, column=2)
generatemenu.title("Generate Key")
generatemenu.mainloop()
mainmenu = tk.Tk()
bg = ImageTk.PhotoImage(file="key.jpg")
background_label = Label(image=bg)
background_label.place(x=0, y=0)
genkeybutton = Button(mainmenu, text= "Generate Key Pair", fg="black",
command=genkeymenu)
encryptbutton = Button(mainmenu, text= "Encrypt your message", fg="black")
decryptbutton = Button(mainmenu, text= "Decrypt your message", fg="black")
background_label.grid(row=0)
genkeybutton.grid(row=0, column=0, sticky = N, rowspan=2)
encryptbutton.grid(row=0, column=0)
decryptbutton.grid(row=0, column=0, sticky=S)
mainmenu.title("RSA ENCRYPTION")
mainmenu.mainloop()

Related

Python tkinter : how to resize widgets when i resize the screen

I'm beginner...
When the code below is executed, how do I make the widgets inside increase when the screen size is increased to width? but, it couldn't resized..
Does it matter if I use either pack or grid?
i can't solve it....
from tkinter import *
from tkinter import ttk
class program:
def __init__(self):
self.root = Tk()
self.root.title("Python")
self.root.resizable(True, True)
self.create_project()
def create_project(self):
Topic_Label = ttk.Label(self.root, text="program")
Topic_Label.pack(side="top")
Record_LabelFrame = LabelFrame(self.root, text="Record information")
Record_LabelFrame.pack(side="top", anchor=W, padx=10)
date = ttk.Label(Record_LabelFrame, text="date")
date.grid(column=0, row=0, sticky=W)
Topic_Label_Entry1 = ttk.Entry(Record_LabelFrame)
Topic_Label_Entry1.grid(column=0, row=1)
time = ttk.Label(Record_LabelFrame, text="time")
time.grid(column=1, row=0, sticky=W)
Topic_Label_Combo1 = ttk.Combobox(Record_LabelFrame)
Topic_Label_Combo1.grid(column=1, row=1)
recorder = ttk.Label(Record_LabelFrame, text="record")
recorder.grid(column=2, row=0, sticky=W)
Topic_Label_Entry2 = ttk.Entry(Record_LabelFrame)
Topic_Label_Entry2.grid(column=2, row=1)
loc = ttk.Label(Record_LabelFrame, text="location")
loc.grid(column=0, row=2, sticky="W")
RadioVar = IntVar()
Radio_Frame = Frame(Record_LabelFrame)
Radio_Frame.grid(column=0, row=3, sticky=W)
Topic_Label_Radio1 = ttk.Radiobutton(Radio_Frame, text="A", variable=RadioVar, value=1)
Topic_Label_Radio1.grid(column=0, row=0)
Topic_Label_Radio2 = ttk.Radiobutton(Radio_Frame, text="B", variable=RadioVar, value=2)
Topic_Label_Radio2.grid(column=1, row=0)
Topic_Label_Radio3 = ttk.Radiobutton(Radio_Frame, text="C", variable=RadioVar, value=3)
Topic_Label_Radio3.grid(column=2, row=0)
count = ttk.Label(Record_LabelFrame, text="count")
count.grid(column=1, row=2, sticky="W")
Topic_Label_Combo2 = ttk.Combobox(Record_LabelFrame)
Topic_Label_Combo2.grid(column=1, row=3)
seed_name = ttk.Label(Record_LabelFrame, text="seed")
seed_name.grid(column=2, row=2, sticky="W")
Topic_Label_Entry4 = ttk.Entry(Record_LabelFrame)
Topic_Label_Entry4.grid(column=2, row=3)
mt = program()
mt.root.mainloop()
I want to print like this that widget resize automatically when i resize the screen
https://i.stack.imgur.com/iSbaJ.png
https://i.stack.imgur.com/mzv9R.png

Why is my Toplevel() window showing blank

I have been on this for some time now, have tried so many methods I could find online but non seems to solve my problem. My Toplevel() window displays nothing. This is supposed to be part of my program at a certain point. Had to copy the code out and summarize it this way yet it doesn't work. I think i am missing something
from tkinter import *
# from tkinter import ttk
# import sqlite3
# conn = sqlite3.connect('shengen.db')
# c = conn.cursor()
# q = c.execute(
# "SELECT email FROM users WHERE email=('ichibuokem#gmail.com');")
# conn.commit()
# for i in q:
# print(list(i))
def portal_start():
portal = Toplevel(root)
portal.title("Visa Application Form")
portal.geometry("600x600")
portal.iconbitmap("...\Visa-Application-Management-System\icon.ico")
Label(portal, text="", bg="grey", height="2",
width="900", font=("Calibri", 14)).pack(pady=10)
spc = Label(portal, text="")
spc.pack()
portal_notebook = ttk.Notebook(portal)
portal_notebook.pack()
page1 = Frame(portal_notebook, width=600, height=600)
page2 = Frame(portal_notebook, width=600, height=600)
page3 = Frame(portal_notebook, width=600, height=600)
page4 = Frame(portal_notebook, width=600, height=600)
summary = Frame(portal_notebook, width=600, height=600)
page1.pack(fill="both", expand=1)
page2.pack(fill="both", expand=1)
page3.pack(fill="both", expand=1)
page4.pack(fill="both", expand=1)
summary.pack(fill="both", expand=1)
portal_notebook.add(page1, text="Basic Details")
portal_notebook.add(page2, text="Sponsorship")
portal_notebook.add(page3, text="Medicals")
portal_notebook.add(page4, text="References")
portal_notebook.add(summary, text="Summary")
# ============================================Portal End===================================================
# =============================================Instantiation window=======================================
def window():
global root
root = Tk()
root.geometry("900x700")
root.title("Welcome Page")
root.iconbitmap("..\Visa-Application-Management-System\icon.ico")
Label(root, text="Welcome To Python Visa Application Portal! \n\nTo check your visa application status, file a new application or update your application, \nLogin or Create an account.",
fg="white", bg="grey", height="6", width="900", font=("Calibri", 14)).pack()
Label(root, text="").pack()
Label(root, text="").pack()
Label(root, text="").pack()
Label(root, text="").pack()
Label(root, text="").pack()
Button(root, text="Login", width=20, font=(
"bold", 14)).pack()
Label(root, text="").pack()
Button(root, text="Create Account", width=20,
font=("bold", 14)).pack()
Label(root, text="").pack()
Label(root, text="").pack()
Label(root, text="Copyright 2020. All Rights Reserved \nWith Luv From Group 3",
font=("Calibri", 8)).pack()
Button(root, text="Test Window", width=20,
font=("bold", 14), command=portal_start).pack()
Label(root, text="").pack()
root.mainloop()
window()
add this to the top your your code
from tkinter import ttk
It should work perfectly then

Image not showing (tkinter in python)

When I try to insert the image into my Tkinter window all that shows up is
this
import tkinter as tk
root = tk.Tk()
root.title("To Do List")
canvas = tk.Canvas(root, height=900, width=1000, bg='white')
frame = tk.Frame(root, bg="#000000")
img = tk.PhotoImage("deathnote.png")
entry = tk.Entry(frame, font='system', fg='white', bg='black')
imglabel = tk.Label(frame, image=img)
canvas.grid(row=0, column=0)
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
imglabel.grid(row=0, column=2)
entry.grid(row=1, column=1)
root.mainloop()
Try using PIL:
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
root.title("To Do List")
canvas = tk.Canvas(root, height=900, width=1000, bg='white')
frame = tk.Frame(root, bg="#000000")
img = ImageTk.PhotoImage(Image.open("deathnote.png"))
entry = tk.Entry(frame, font='system', fg='white', bg='black')
imglabel = tk.Label(frame, image=img)
canvas.grid(row=0, column=0)
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
imglabel.grid(row=0, column=2)
entry.grid(row=1, column=1)
root.mainloop()

Tkinter update label text

I am trying to update the label text in "label_enter_what" in accordance to what they chose in "drop". So if they choose "Energy", the label would change to: Enter wavelength in chosen unit below", for example. Sorry if the code looks messy, it's my first time coding. This is supposed to be a photon property calculator that i am making for fun because in physics we are currently doing this, but with pens and calculators.
import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
HEIGHT = 600
WIDTH = 900
root = tk.Tk()
root.title("Photon property calculator")
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_image = ImageTk.PhotoImage(Image.open('interference.jpg'))
background_label = tk.Label(root, image=background_image)
background_label.place(relheight=1, relwidth=1)
frame = tk.Frame(root, bg='#3E3E3E', bd=5)
frame.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.75, anchor='n')
frame_upper = tk.Frame(root, bg='#3E3E3E', bd=5)
frame_upper.place(relx=0.5, rely=0.03, relheight=0.06, relwidth=0.75, anchor='n')
label_what_to_calc = tk.Label(frame_upper, bg='white', text='Enter what \n to calculate')
label_what_to_calc.place(relx=0, rely=0, relheight=1, relwidth=0.15)
label_enter_what = tk.Label(frame_upper, bg='white', text='Enter * in a chosen unit below.')
label_enter_what.place(relx=0.2, relheight=1, relwidth=0.2)
label_unit = tk.Label(frame_upper, bg='white', text='')
label_unit.place(relx=0.5, relheight=1, relwidth=0.1)
lower_frame = tk.Frame(root, bg='#60A8FF', bd=10)
lower_frame.place(relx=0.5, rely=0.3, relheight=0.5, relwidth=0.75, anchor='n')
entry_value = tk.Entry(frame, font=40, bg='white')
entry_value.place(relx=0.175, rely=0, relheight=1, relwidth=0.4)
OPTIONS = [
"Energy",
"Frequency",
"Wavelength"
]
clicked = StringVar()
clicked.set(OPTIONS[0])
drop = tk.OptionMenu(frame, clicked, *OPTIONS)
drop.place(relx=0, rely=0, relheight=1, relwidth=0.15)
OPTIONS_UNITS = ["μm",
"nm",
"pm",
"aJ",
"zJ",
]
clicked_1 = StringVar()
clicked_1.set(OPTIONS_UNITS[1])
drop_units = tk.OptionMenu(frame, clicked_1, *OPTIONS_UNITS)
drop_units.place(relx=0.6, rely=0, relheight=1, relwidth=0.09)
button = tk.Button(frame, text='Calculate!', font=40, bg="#F96612", fg='black')
button.place(relx=0.7, relheight=1, relwidth=0.3)
label = tk.Label(lower_frame, bg='white', text=clicked.get())
label.place(relheight=1, relwidth=1)
root.mainloop()
You can use
tk.OptionMenu( ... command=function)
to run function which will get selected value and it will change text in label
import tkinter as tk
# --- functions ---
def on_select(value):
label['text'] = value
# --- main ---
root = tk.Tk()
label = tk.Label(root, text='?')
label.pack()
OPTIONS = ["Energy", "Frequency", "Wavelength"]
value_var = tk.StringVar(value=OPTIONS[0])
op = tk.OptionMenu(root, value_var, *OPTIONS, command=on_select)
op.pack()
root.mainloop()

How to design GUI properly

I'm new to python and I don't understand how do I configure the resolution of the font, I wanted to design it but it keeps on expanding, because of the grid(?), Thanks in advance
Here's my code:
import tkinter
from tkinter import ttk
selection=('Weight','Volume','Length')
length = ('mm', 'cm', 'inches','feet', 'yards', 'meter', 'km', 'miles')
weight=('mg','lb','g','kg','oz','ton')
volume=('ml','l','gal','pt','qt')
#title
window = tkinter.Tk()
window.title("Conversion APP")
window.configure(background='#a1dbcd')
title=tkinter.Label(window,text='Welcome to the Converter APP!', fg="#383a39", bg="#a1dbcd", font=("Helvetica",16))
title.grid(row=0,column=2)
title.config(width=100)
window.geometry("300x200")
#1st
labelOne = ttk.Label(window,text='Enter Value',background='blue')
labelOne.grid(row=1,column=0)
to_be_converted = ttk.Combobox(values=length, width=10)
to_be_converted.grid(row=1, column=2)
#2nd
labelTwo = ttk.Label(window, text="Convert To",background='blue')
labelTwo.grid(row=2,column=0)
converted = ttk.Combobox(values=length, width=10)
converted.grid(row=2, column=2)
#input
userName = tkinter.DoubleVar()
userEntry = ttk.Entry(window, width=5, textvariable = userName)
userEntry.grid(row=1, column=1)
#button to convert
btn = ttk.Button(window, text='Convert!', command=convert_length or convert_weight or convert_volume)
btn.grid(row=1, column=4)
#button to exit
exit= ttk.Button(window, text='Quit', command=exit_converter)
exit.grid(row=2, column=4)
window.mainloop()

Categories