How to design GUI properly - python

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()

Related

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

why is .grid giving me an error when I am trying to move my button

import time
import tkinter as tk
from tkinter import scrolledtext
win = tk.Tk()
win.title("My First Game")
win.configure(bg="black")
win.geometry("640x400")
label = tk.Label(win, text="test", fg="red", bg="black").pack()
canvas1 = tk.Canvas(win, width=130, height=20)
canvas1.pack()
entry1 = tk.Entry(win, font="Helvetica 10")
canvas1.create_window(65, 10, window=entry1)
entry1.insert(0, "Type here")
def shortcut():
Shortcut = tk.Label(win, fg="red", bg="black", text="test2")
Shortcut.pack()
button1 = tk.Button(win, text="Enter", fg="red", bg="black", command=shortcut)
button1.pack()
exit_button = tk.Button(win, text="Quit", padx=4, pady=2, bg="black", fg="red", command=quit)
exit_button.pack()
exit_button.grid(row=0, column=2)
win.mainloop()
Why is this giving me an error? I tried in a separate project with just a black screen and the button and it worked fine. But when I put it in the code above it doesn't work
line 42, in <module> exit_button.grid(row=0, column=2)
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
As #10Rep mentioned in comment - you can remove grid() to resolve problem with error.
import tkinter as tk
# --- functions ---
def shortcut():
shortcut = tk.Label(win, text="test2")
shortcut.pack()
# --- main ---
win = tk.Tk()
label = tk.Label(win, text="test")
label.pack()
canvas1 = tk.Canvas(win) #, width=130, height=20)
canvas1.pack()
entry1 = tk.Entry(canvas1)
canvas1.create_window(0, 0, window=entry1, anchor='nw')
entry1.insert(0, "Type here")
button1 = tk.Button(win, text="Enter", command=shortcut)
button1.pack()
exit_button = tk.Button(win, text="Quit", command=win.destroy)
exit_button.pack()
win.mainloop()
But I expect that you used grid() to organize two buttons in one line.
Problem is that you can't mix pack() and grid() in one window/frame and I see two solutions:
First is to use only grid() to organize all widgets
import tkinter as tk
# --- functions ---
def shortcut():
shortcut = tk.Label(win, text="test2")
shortcut.grid(row=3, column=0, columnspan=2)
# --- main ---
win = tk.Tk()
label = tk.Label(win, text="test")
label.grid(row=0, column=0, columnspan=2)
canvas1 = tk.Canvas(win) #, width=130, height=20)
canvas1.grid(row=1, column=0, columnspan=2)
entry1 = tk.Entry(canvas1)
canvas1.create_window(0, 0, window=entry1, anchor='nw')
entry1.insert(0, "Type here")
button1 = tk.Button(win, text="Enter", command=shortcut)
button1.grid(row=2, column=0)
exit_button = tk.Button(win, text="Quit", command=win.destroy)
exit_button.grid(row=2, column=1)
win.mainloop()
Second is to put Frame (using pack()) and put buttons inside this frame using grid()
import tkinter as tk
# --- functions ---
def shortcut():
shortcut = tk.Label(win, text="test2")
shortcut.pack()
# --- main ---
win = tk.Tk()
label = tk.Label(win, text="test")
label.pack()
canvas1 = tk.Canvas(win) #, width=130, height=20)
canvas1.pack()
entry1 = tk.Entry(canvas1)
canvas1.create_window(0, 0, window=entry1, anchor='nw')
entry1.insert(0, "Type here")
# - frame with grid -
f = tk.Frame(win)
f.pack()
button1 = tk.Button(f, text="Enter", command=shortcut)
button1.grid(row=0, column=0)
exit_button = tk.Button(f, text="Quit", command=win.destroy)
exit_button.grid(row=0, column=1)
# -
win.mainloop()
or using pack(side=...)
import tkinter as tk
# --- functions ---
def shortcut():
shortcut = tk.Label(win, text="test2")
shortcut.pack()
# --- main ---
win = tk.Tk()
label = tk.Label(win, text="test")
label.pack()
canvas1 = tk.Canvas(win) #, width=130, height=20)
canvas1.pack()
entry1 = tk.Entry(canvas1)
canvas1.create_window(0, 0, window=entry1, anchor='nw')
entry1.insert(0, "Type here")
# - frame with pack(side=...) -
f = tk.Frame(win)
f.pack()
button1 = tk.Button(f, text="Enter", command=shortcut)
button1.pack(side='left')
exit_button = tk.Button(f, text="Quit", command=win.destroy)
exit_button.pack(side='left')
# -
win.mainloop()

How to make .configure(text="lorem ipsum") only display in a given amount of time (e.g. only shows for 5 seconds)?

Right now, for instance if I click the "copy" button for Username, the "Username copied!" will display there forever. I only want it to display for 5 seconds and the "Welcome" in the description = Label(window, text="Welcome") will show again.
from tkinter import *
from urllib import parse
from tkinter import Tk
window = Tk()
window.title("CopyText")
window.geometry('295x150+600+210')
large_font = ('Verdana',11)
small_font = ('Verdana',10)
#USERNAME ===================
lbl = Label(window, text="Username:")
lbl.grid(column=0, row=0)
entry1Var = StringVar(value='myusername')
txt = Entry(window,textvariable=entry1Var,font=large_font, width=18)
txt.grid(column=1, row=0)
def clicked():
copy = txt.get()
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(copy)
description.configure(text="Username copied!")
r.update()
btn = Button(window, text="copy", command=clicked, height=2, width=5)
btn.grid(column=2, row=0)
#PASSWORD ===================
pword = Label(window, text="Password:")
pword.grid(column=0, row=4)
entry2Var = StringVar(value='mypassword')
pwordtxt = Entry(window,textvariable=entry2Var,font=large_font, width=18)
pwordtxt.grid(column=1, row=4)
def clicked():
copy = pwordtxt.get()
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(copy)
description.configure(text="Password copied!")
r.update()
btn = Button(window, text="copy", command=clicked, height=2, width=5)
btn.grid(column=2, row=4)
#EMAIL ===================
email = Label(window, text="Email:")
email.grid(column=0, row=6)
entry3Var = StringVar(value='myemail#gmail.com')
emailtxt = Entry(window,textvariable=entry3Var,font=large_font, width=18)
emailtxt.grid(column=1, row=6)
def clicked():
copy = emailtxt.get()
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(copy)
description.configure(text= "Email copied!")
r.update()
btn = Button(window, text="copy", command=clicked, height=2, width=5)
btn.grid(column=2, row=6)
description = Label(window, text="Welcome")
description.grid(column=1, row=7)
window.mainloop()

AttributeError in Tkinter while working with control variables in radiobuttons

I'm trying to get the value of the radiobutton selcted and storing this int into a varable. It's my first tkinter project, so I'm sorry for my probably stupid mistakes...
from tkinter import *
from tkinter import ttk
select = "A"
def begin():
grand = Tk()
grand.title("Converter")
window.destroy()
frame = Frame(grand)
option = IntVar()
-> AttributeError: 'NoneType' object has no attribute '_root'
grandlabel = Label(frame, text="Choose the grand").grid(row=0, sticky=N, padx=5)
grand1 = Radiobutton(frame, text="Speed", variable=option, value=1, command=sel).grid(row=1, sticky=W)
grand2 = Radiobutton(frame, text="etc", variable=option, value=2, command=sel).grid(row=2, sticky=W)
submitgrand = Button(frame, text="Ok", command=unit).grid(row=3, sticky=W)
frame.pack()
grand.mainloop()
def sel():
global option
global select
select = option.get()
option = StringVar()
def unit():
unit = Tk()
global select
select = grandchosen
if (grandchosen == "Speed"):
Label(unit, text="Test").pack()
else:
Label(unit, text="Test2").pack()
unit.mainloop()
root = Tk()
frame = Frame(root)
welcome = ttk.Label(frame, text="Welcome!").grid(row=0, sticky=N, padx=10, pady=3)
okbutton = Button(frame, text="Ok", width=15, command=begin).grid(row=1, sticky=S, padx=20, pady=30)
frame.pack()
style = ttk.Style()
style.configure("TLabel", foreground="midnight blue", font="Times 19")
root.mainloop()
Would be great to get some help, thank you!

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

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()

Categories