Align input labels and results using Tkinter (Python) - python

I want to align these input labels to the left and align the results label to the center of the interface as shown in the image.
I also welcome suggestions on how I can build a better interface for the program in question.
Interface example
import tkinter as tk
import math
from tkinter import *
def peso_proprio(gama_c, bm, H, hb, B, phi, gama_s):
gpar_k = float(gama_c) * float(bm)
Gm1_k = float(gpar_k) * float(H)
Gm2_k = float(gama_c) * float(bm) * float(hb)
gb_k = float(gama_c) * float(hb)
Gb_k = float(gama_c) * float(hb) * float(B)
sigma_vs = float(gama_s) * float(H)
G_sk = float(sigma_vs) * float(B)
K = math.tan(math.radians(45 - (float(phi) / 2))) ** 2
hs1 = float(K) * float(gama_s) * float(H)
hs2 = float(K) * float(gama_s) * (float(H) + float(hb))
Hs1_k = (float(hs1) * float(H)) / 2
Hs2_k = (float(K) * float(gama_s) * float(hb) * (2 * float(H) + float(hb))) / 2
Hs_k = float(Hs1_k) + float(Hs2_k)
mi_e = math.tan(math.radians(float(phi) * 2 / 3))
FS_d = (float(mi_e) * (float(Gm1_k) + float(Gm2_k) + float(Gb_k) + float(G_sk))) / float(Hs_k)
FS_t = ((float(Gm1_k) * float(Gm2_k)*(float(bm)/2)) + ((float(Gb_k) + float(G_sk))*(float(bm)+(float(B)/2))))/(float(Hs_k)*((float(H)+float(hb))/3))
fat_k = float(Hs_k) / (float(B) + float(bm))
Nbase_k = float(Gm1_k) + float(Gm2_k) + float(Gb_k) + float(G_sk)
Mbase_k = float(Hs_k) * (float(H) + float(hb)) / 3 + (float(Gm1_k) + float(Gm2_k)) * (float(B) / 2) - (float(Gb_k) + float(G_sk)) * (float(hb) / 2)
A = float(B) + float(bm)
W = ((float(B) + float(bm)) ** 2) / 6
e = float(Mbase_k) / float(Nbase_k)
e_lim = float(A) / 6
sigma_sk_max = (float(Nbase_k) / float(A)) + (float(Mbase_k) / float(W))
sigma_sk_min = (float(Nbase_k) / float(A)) - (float(Mbase_k) / float(W))
return gpar_k, Gm1_k, Gm2_k, gb_k, Gb_k, sigma_vs, G_sk, K, hs1, hs2, Hs1_k, Hs2_k, Hs_k, mi_e, FS_d, FS_t, fat_k, Nbase_k, Mbase_k, A, W, e, e_lim, sigma_sk_max, sigma_sk_min
def calculate():
gama_c = gama_c_entry.get()
bm = bm_entry.get()
H = H_entry.get()
hb = hb_entry.get()
B = B_entry.get()
gama_s = gama_s_entry.get()
phi = phi_entry.get()
SPT = SPT_entry.get()
Qsc_k = Qsc_k_entry.get()
result_pp = peso_proprio(gama_c, bm, H, hb, B, phi, gama_s)
result_label.config(text=f'\nPESO PRÓPRIO\n\ngpar k = {result_pp[0]} kN/m, \nGm1 k = {result_pp[1]} kN, \nGm2 k = {result_pp[2]} kN, \ngb k = {result_pp[3]} kN/m,\nGb k = {result_pp[4]} kN, \nσ vs = {result_pp[5]} Pa,\nGsk = {result_pp[6]}, \nK = {result_pp[7]}, \nhs1 = {result_pp[8]}, \nhs2 = {result_pp[9]}, \nHs1 k = {result_pp[10]}, \nHs2 k = {result_pp[11]}, \nHs k = {result_pp[12]}, \nFS DESLIZAMENTO = {result_pp[13]}, \nFS TOMBAMENTO = {result_pp[14]}, \nFat k = {result_pp[15]}, \nN base = {result_pp[16]}, \nM base = {result_pp[17]},\nÁrea da base = {result_pp[18]} m², \nW = {result_pp[19]} m³, \ne = {result_pp[20]} m,\ne limite = {result_pp[21]} m, \nσsk máx. = {result_pp[22]} kN/m², \nσsk míx. = {result_pp[23]} kN/m²')
root = tk.Tk()
title_label = tk.Label(root, text="MURO DE ARRIMO EM L", font=("Microsoft YaHei", 18))
title_label.pack()
variaveis_label = tk.Label(root, text="\nVARIÁVEIS DE ENTRADA")
variaveis_label.pack()
gama_c_label = tk.Label(root, text="\nEntre γc (kN/m³): ")
gama_c_label.pack()
gama_c_entry = tk.Entry(root)
gama_c_entry.pack()
bm_label = tk.Label(root, text="Entre bm (m): ")
bm_label.pack()
bm_entry = tk.Entry(root)
bm_entry.pack()
H_label = tk.Label(root, text="Entre H (m): ")
H_label.pack()
H_entry = tk.Entry(root)
H_entry.pack()
hb_label = tk.Label(root, text="Entre hb (m): ")
hb_label.pack()
hb_entry = tk.Entry(root)
hb_entry.pack()
B_label = tk.Label(root, text="Entre B (m): ")
B_label.pack()
B_entry = tk.Entry(root)
B_entry.pack()
gama_s_label = tk.Label(root, text="Entre γs (kN/m³): ")
gama_s_label.pack()
gama_s_entry = tk.Entry(root)
gama_s_entry.pack()
phi_label = tk.Label(root, text="Entre φ (°): ")
phi_label.pack()
phi_entry = tk.Entry(root)
phi_entry.pack()
SPT_label = tk.Label(root, text="Entre SPT Médio: ")
SPT_label.pack()
SPT_entry = tk.Entry(root)
SPT_entry.pack()
Qsc_k_label = tk.Label(root, text="Entre sobrecarga (kN): ")
Qsc_k_label.pack()
Qsc_k_entry = tk.Entry(root)
Qsc_k_entry.pack()
calculate_button = tk.Button(root, text="CALCULAR", command=calculate)
calculate_button.pack()
result_label = tk.Label(root, text="")
result_label.pack()
#criar um objeto PhotoImage
image = PhotoImage(file="C:/Users/Usuário/Desktop/Muro_Arrimo.png")
#criar label para mostrar a imagem
image_label = tk.Label(root, image=image)
image_label = tk.Label(root, image=image)
image_label.place(relx=1, x=-10, y=10, anchor="ne")
root.mainloop()
I already tried using the grid method but I think I didn't apply it correctly because all the labels are misaligned like the example below:
root = tk.Tk()
title_label = tk.Label(root, text="MURO DE ARRIMO EM L", font=("Microsoft YaHei", 18))
title_label.grid(row=0, column=0, columnspan=2, pady=5)
variaveis_label = tk.Label(root, text="VARIÁVEIS DE ENTRADA")
variaveis_label.grid(row=1, column=0, columnspan=2, pady=5)
gama_c_label = tk.Label(root, text="Entre γc (kN/m³): ")
gama_c_label.grid(row=2, column=0, pady=5)
gama_c_entry = tk.Entry(root)
gama_c_entry.grid(row=2, column=1, pady=5)

Using tk.Frame makes placement easier. Sorry my english is not good so it's hard to explain.
import tkinter as tk
import math
from tkinter import *
def peso_proprio(gama_c, bm, H, hb, B, phi, gama_s):
gpar_k = float(gama_c) * float(bm)
Gm1_k = float(gpar_k) * float(H)
Gm2_k = float(gama_c) * float(bm) * float(hb)
gb_k = float(gama_c) * float(hb)
Gb_k = float(gama_c) * float(hb) * float(B)
sigma_vs = float(gama_s) * float(H)
G_sk = float(sigma_vs) * float(B)
K = math.tan(math.radians(45 - (float(phi) / 2))) ** 2
hs1 = float(K) * float(gama_s) * float(H)
hs2 = float(K) * float(gama_s) * (float(H) + float(hb))
Hs1_k = (float(hs1) * float(H)) / 2
Hs2_k = (float(K) * float(gama_s) * float(hb) * (2 * float(H) + float(hb))) / 2
Hs_k = float(Hs1_k) + float(Hs2_k)
mi_e = math.tan(math.radians(float(phi) * 2 / 3))
FS_d = (float(mi_e) * (float(Gm1_k) + float(Gm2_k) + float(Gb_k) + float(G_sk))) / float(Hs_k)
FS_t = ((float(Gm1_k) * float(Gm2_k)*(float(bm)/2)) + ((float(Gb_k) + float(G_sk))*(float(bm)+(float(B)/2))))/(float(Hs_k)*((float(H)+float(hb))/3))
fat_k = float(Hs_k) / (float(B) + float(bm))
Nbase_k = float(Gm1_k) + float(Gm2_k) + float(Gb_k) + float(G_sk)
Mbase_k = float(Hs_k) * (float(H) + float(hb)) / 3 + (float(Gm1_k) + float(Gm2_k)) * (float(B) / 2) - (float(Gb_k) + float(G_sk)) * (float(hb) / 2)
A = float(B) + float(bm)
W = ((float(B) + float(bm)) ** 2) / 6
e = float(Mbase_k) / float(Nbase_k)
e_lim = float(A) / 6
sigma_sk_max = (float(Nbase_k) / float(A)) + (float(Mbase_k) / float(W))
sigma_sk_min = (float(Nbase_k) / float(A)) - (float(Mbase_k) / float(W))
return gpar_k, Gm1_k, Gm2_k, gb_k, Gb_k, sigma_vs, G_sk, K, hs1, hs2, Hs1_k, Hs2_k, Hs_k, mi_e, FS_d, FS_t, fat_k, Nbase_k, Mbase_k, A, W, e, e_lim, sigma_sk_max, sigma_sk_min
def calculate():
gama_c = gama_c_entry.get()
bm = bm_entry.get()
H = H_entry.get()
hb = hb_entry.get()
B = B_entry.get()
gama_s = gama_s_entry.get()
phi = phi_entry.get()
SPT = SPT_entry.get()
Qsc_k = Qsc_k_entry.get()
result_pp = peso_proprio(gama_c, bm, H, hb, B, phi, gama_s)
result_label.config(text=f'\nPESO PRÓPRIO\n\ngpar k = {result_pp[0]} kN/m, \nGm1 k = {result_pp[1]} kN, \nGm2 k = {result_pp[2]} kN, \ngb k = {result_pp[3]} kN/m,\nGb k = {result_pp[4]} kN, \nσ vs = {result_pp[5]} Pa,\nGsk = {result_pp[6]}, \nK = {result_pp[7]}, \nhs1 = {result_pp[8]}, \nhs2 = {result_pp[9]}, \nHs1 k = {result_pp[10]}, \nHs2 k = {result_pp[11]}, \nHs k = {result_pp[12]}, \nFS DESLIZAMENTO = {result_pp[13]}, \nFS TOMBAMENTO = {result_pp[14]}, \nFat k = {result_pp[15]}, \nN base = {result_pp[16]}, \nM base = {result_pp[17]},\nÁrea da base = {result_pp[18]} m², \nW = {result_pp[19]} m³, \ne = {result_pp[20]} m,\ne limite = {result_pp[21]} m, \nσsk máx. = {result_pp[22]} kN/m², \nσsk míx. = {result_pp[23]} kN/m²')
root = tk.Tk()
input_frame=tk.Frame(root)
input_frame.pack(side="left", expand=1, fill="both")
output_frame=tk.Frame(root)
output_frame.pack(side="left", expand=1, fill="both")
pic_frame=tk.Frame(root)
pic_frame.pack(side="left", expand=1, fill="both")
title_label = tk.Label(input_frame, text="MURO DE ARRIMO EM L", font=("Microsoft YaHei", 18))
title_label.pack()
variaveis_label = tk.Label(input_frame, text="\nVARIÁVEIS DE ENTRADA")
variaveis_label.pack()
gama_c_label = tk.Label(input_frame, text="\nEntre γc (kN/m³): ")
gama_c_label.pack()
gama_c_entry = tk.Entry(input_frame)
gama_c_entry.pack()
bm_label = tk.Label(input_frame, text="Entre bm (m): ")
bm_label.pack()
bm_entry = tk.Entry(input_frame)
bm_entry.pack()
H_label = tk.Label(input_frame, text="Entre H (m): ")
H_label.pack()
H_entry = tk.Entry(input_frame)
H_entry.pack()
hb_label = tk.Label(input_frame, text="Entre hb (m): ")
hb_label.pack()
hb_entry = tk.Entry(input_frame)
hb_entry.pack()
B_label = tk.Label(input_frame, text="Entre B (m): ")
B_label.pack()
B_entry = tk.Entry(input_frame)
B_entry.pack()
gama_s_label = tk.Label(input_frame, text="Entre γs (kN/m³): ")
gama_s_label.pack()
gama_s_entry = tk.Entry(input_frame)
gama_s_entry.pack()
phi_label = tk.Label(input_frame, text="Entre φ (°): ")
phi_label.pack()
phi_entry = tk.Entry(input_frame)
phi_entry.pack()
SPT_label = tk.Label(input_frame, text="Entre SPT Médio: ")
SPT_label.pack()
SPT_entry = tk.Entry(input_frame)
SPT_entry.pack()
Qsc_k_label = tk.Label(input_frame, text="Entre sobrecarga (kN): ")
Qsc_k_label.pack()
Qsc_k_entry = tk.Entry(input_frame)
Qsc_k_entry.pack()
calculate_button = tk.Button(input_frame, text="CALCULAR", command=calculate)
calculate_button.pack()
result_label = tk.Label(output_frame, text="")
result_label.pack()
#criar um objeto PhotoImage
image = PhotoImage(file="C:/Users/Usuário/Desktop/Muro_Arrimo.png")
#criar label para mostrar a imagem
image_label = tk.Label(pic_frame, image=image)
image_label.pack()
root.mainloop()

Related

Traceback (most recent call last): _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack in python

I have a problem with my code in Python
I want to transfer the data after entering it into the excel file
But I get an error with the title: Traceback (most recent call last):
File "C:\Users\yasen\Desktop\برمجة\graphics\main11.py", line 60, in
firstname_text = Label(text = "الاسم الاول * ",).grid(row=0)
File "C:\Users\yasen\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 2522, in grid_configure
self.tk.call(
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
my code
from tkinter import *
import pandas as pd
def send_info():
path = 'Registers.form.xlsx'
df1 = pd.read_excel(path)
SeriesA = df1['firstname']
SeriesB = df1['secondname']
SeriesC = df1['thirdname']
SeriesD = df1['age']
SeriesE = df1['phonenumber']
SeriesF = df1['Departmentname']
SeriesG = df1['familymembers']
SeriesH = df1['bookingdate']
A = pd.Series(firstname.get)
B = pd.Series(secondname.get)
C = pd.Series(thirdname.get)
D = pd.Series(age.get)
E = pd.Series(phonenumber.get)
F = pd.Series(Departmentname.get)
G = pd.Series(familymembers.get)
H = pd.Series(bookingdate.get)
SeriesA = SeriesA.append(A)
SeriesB = SeriesA.append(B)
SeriesC = SeriesA.append(C)
SeriesD = SeriesA.append(D)
SeriesE = SeriesA.append(E)
SeriesF = SeriesA.append(F)
SeriesG = SeriesA.append(G)
SeriesH = SeriesA.append(H)
df2 = pd.DataFrame({"firstname": SeriesA, "secondnamer": SeriesB, "thirdname": SeriesC, "age": SeriesD, "phonenumber": SeriesE, "Departmentname": SeriesF, "familymembers": SeriesG, "bookingdate": SeriesH})
df2.to_excel(path, index=False)
firstname_entry.delete(0, END)
secondname_entry.delete(0, END)
thirdname_entry.delete(0, END)
age_entry.delete(0, END)
phonenumber_entry.delete(0, END)
Departmentname_entry.delete(0, END)
familymembers_entry.delete(0, END)
bookingdate_entry.delete(0, END)
screen = Tk()
screen.geometry("1080x1080")
screen.title("مديرية شؤون البطاقة الوطنية")
welcome_text = Label(text="أهلا وسهلا بكم في الحجز الالكتروني للبطاقة الموحدة ", fg="white", bg="green",height=2 ,width=1080)
welcome_text.pack()
firstname_text = Label(text = "الاسم الاول * ",).grid(row=0)
secondname_text = Label(text = "الاسم الثاني * ",).grid(row=1)
thirdname_text = Label(text = "الاسم الثالث * ",).grid(row=2)
age_text = Label(text = "العمر * ",).grid(row=3)
phonenumber_text = Label(text = "رقم الهاتف * ",).grid(row=4)
Departmentname_text = Label(text = "اسم دائرة الاحوال المدنية * ",).grid(row=5)
familymembers_text = Label(text = "عدد افراد الاسرة * ",).grid(row=6)
bookingdate_text = Label(text = "موعد تاريخ الحجز المطلوب * ",).grid(row=7)
firstname_text.place(x = 540, y = 70)
secondname_text.place(x = 540, y = 130)
thirdname_text.place(x = 540, y = 190)
age_text.place(x = 540, y = 250)
phonenumber_text.place(x = 540, y = 310)
Departmentname_text.place(x = 540, y = 370)
familymembers_text.place(x = 540, y = 430)
bookingdate_text.place(x = 540, y = 490)
firstname = StringVar()
secondname = StringVar()
thirdname = StringVar()
age = StringVar()
phonenumber = StringVar()
Departmentname = StringVar()
familymembers = StringVar()
bookingdate = StringVar()
firstname_entry = Entry(textvariable = firstname, width = "40")
secondname_entry = Entry(textvariable = secondname, width = "40")
thirdname_entry = Entry(textvariable = thirdname, width = "40")
age_entry = Entry(textvariable = age, width = "40")
phonenumber_entry = Entry(textvariable = phonenumber, width = "40")
Departmentname_entry = Entry(textvariable = Departmentname, width = "40")
familymembers_entry = Entry(textvariable = familymembers, width = "40")
bookingdate_entry = Entry(textvariable = bookingdate, width = "40")
firstname_entry.place(x = 450, y = 100)
secondname_entry.place(x = 450, y = 160)
thirdname_entry.place(x = 450, y = 220)
age_entry.place(x = 450, y = 280)
phonenumber_entry.place(x = 450, y = 340)
Departmentname_entry.place(x = 450, y = 400)
familymembers_entry.place(x = 450, y = 460)
bookingdate_entry.place(x = 450, y = 520)
firstname_entry.grid(row=0,column=1)
secondname_entry.grid(row=1,column=1)
thirdname_entry.grid(row=2,column=1)
age_entry.grid(row=3,column=1)
phonenumber_entry.grid(row=4,column=1)
Departmentname_entry.grid(row=5,column=1)
familymembers_entry.grid(row=6,column=1)
bookingdate_entry.grid(row=7,column=1)
register = Button(screen,text = "Register", width = "60", height = "2", command = send_info, bg = "grey").grid(row=3,column=1, pady=4)
register.place(x = 360, y = 600)
screen.mainloop ()
As I just said on FB - you cant miy .place, .pack. and .grid
from tkinter import *
import pandas as pd
def send_info():
path = 'Registers.form.xlsx'
df1 = pd.read_excel(path)
SeriesA = df1['firstname']
SeriesB = df1['secondname']
SeriesC = df1['thirdname']
SeriesD = df1['age']
SeriesE = df1['phonenumber']
SeriesF = df1['Departmentname']
SeriesG = df1['familymembers']
SeriesH = df1['bookingdate']
A = pd.Series(firstname.get)
B = pd.Series(secondname.get)
C = pd.Series(thirdname.get)
D = pd.Series(age.get)
E = pd.Series(phonenumber.get)
F = pd.Series(Departmentname.get)
G = pd.Series(familymembers.get)
H = pd.Series(bookingdate.get)
SeriesA = SeriesA.append(A)
SeriesB = SeriesA.append(B)
SeriesC = SeriesA.append(C)
SeriesD = SeriesA.append(D)
SeriesE = SeriesA.append(E)
SeriesF = SeriesA.append(F)
SeriesG = SeriesA.append(G)
SeriesH = SeriesA.append(H)
df2 = pd.DataFrame(
{"firstname": SeriesA, "secondnamer": SeriesB, "thirdname": SeriesC, "age": SeriesD, "phonenumber": SeriesE,
"Departmentname": SeriesF, "familymembers": SeriesG, "bookingdate": SeriesH})
df2.to_excel(path, index=False)
firstname_entry.delete(0, END)
secondname_entry.delete(0, END)
thirdname_entry.delete(0, END)
age_entry.delete(0, END)
phonenumber_entry.delete(0, END)
Departmentname_entry.delete(0, END)
familymembers_entry.delete(0, END)
bookingdate_entry.delete(0, END)
screen = Tk()
screen.geometry("1080x1080")
screen.title("مديرية شؤون البطاقة الوطنية")
welcome_text = Label(text="أهلا وسهلا بكم في الحجز الالكتروني للبطاقة الموحدة ", fg="white", bg="green", height=2,
width=1080)
firstname_text = Label(text="الاسم الاول * ", )
secondname_text = Label(text="الاسم الثاني * ", )
thirdname_text = Label(text="الاسم الثالث * ", )
age_text = Label(text="العمر * ", )
phonenumber_text = Label(text="رقم الهاتف * ", )
Departmentname_text = Label(text="اسم دائرة الاحوال المدنية * ", )
familymembers_text = Label(text="عدد افراد الاسرة * ", )
bookingdate_text = Label(text="موعد تاريخ الحجز المطلوب * ", )
firstname = StringVar()
firstname.set("Test")
secondname = StringVar()
thirdname = StringVar()
age = StringVar()
phonenumber = StringVar()
Departmentname = StringVar()
familymembers = StringVar()
bookingdate = StringVar()
firstname_entry = Entry(textvariable=firstname, width=40)
secondname_entry = Entry(textvariable=secondname, width=40)
thirdname_entry = Entry(textvariable=thirdname, width=40)
age_entry = Entry(textvariable=age, width=40)
phonenumber_entry = Entry(textvariable=phonenumber, width=40)
Departmentname_entry = Entry(textvariable=Departmentname, width=40)
familymembers_entry = Entry(textvariable=familymembers, width=40)
bookingdate_entry = Entry(textvariable=bookingdate, width=40)
firstname_entry.grid(row=0, column=1)
secondname_entry.grid(row=1, column=1)
thirdname_entry.grid(row=2, column=1)
age_entry.grid(row=3, column=1)
phonenumber_entry.grid(row=4, column=1)
Departmentname_entry.grid(row=5, column=1)
familymembers_entry.grid(row=6, column=1)
bookingdate_entry.grid(row=7, column=1)
'''register = Button(screen, text="Register", width="60", height="2", command=send_info, bg="grey").grid(row=3, column=1,
pady=4)
register.place(x=360, y=600)'''
screen.mainloop()
This is what you want, like this. You see Label on green background and gray button. I remove all widgets grid() because of that saying cannot use geometry manager grid inside . which already has slaves managed by pack
Code:
from tkinter import *
import pandas as pd
def send_info():
path = 'Registers.form.xlsx'
df1 = pd.read_excel(path)
SeriesA = df1['firstname']
SeriesB = df1['secondname']
SeriesC = df1['thirdname']
SeriesD = df1['age']
SeriesE = df1['phonenumber']
SeriesF = df1['Departmentname']
SeriesG = df1['familymembers']
SeriesH = df1['bookingdate']
A = pd.Series(firstname.get)
B = pd.Series(secondname.get)
C = pd.Series(thirdname.get)
D = pd.Series(age.get)
E = pd.Series(phonenumber.get)
F = pd.Series(Departmentname.get)
G = pd.Series(familymembers.get)
H = pd.Series(bookingdate.get)
SeriesA = SeriesA.append(A)
SeriesB = SeriesA.append(B)
SeriesC = SeriesA.append(C)
SeriesD = SeriesA.append(D)
SeriesE = SeriesA.append(E)
SeriesF = SeriesA.append(F)
SeriesG = SeriesA.append(G)
SeriesH = SeriesA.append(H)
df2 = pd.DataFrame({"firstname": SeriesA, "secondnamer": SeriesB, "thirdname": SeriesC, "age": SeriesD, "phonenumber": SeriesE, "Departmentname": SeriesF, "familymembers": SeriesG, "bookingdate": SeriesH})
df2.to_excel(path, index=False)
firstname_entry.delete(0, END)
secondname_entry.delete(0, END)
thirdname_entry.delete(0, END)
age_entry.delete(0, END)
phonenumber_entry.delete(0, END)
Departmentname_entry.delete(0, END)
familymembers_entry.delete(0, END)
bookingdate_entry.delete(0, END)
screen = Tk()
screen.geometry("1080x1080")
screen.title("مديرية شؤون البطاقة الوطنية")
welcome_text = Label(text="أهلا وسهلا بكم في الحجز الالكتروني للبطاقة الموحدة ", fg="white", bg="green",height=2 ,width=1080)
welcome_text.pack()
firstname_text = Label(text="الاسم الاول * ")
secondname_text = Label(text="الاسم الثاني * ")
thirdname_text = Label(text="الاسم الثالث * ")
age_text = Label(text="العمر * ")
phonenumber_text = Label(text="رقم الهاتف * ")
Departmentname_text = Label(text="اسم دائرة الاحوال المدنية * ")
familymembers_text = Label(text="عدد افراد الاسرة * ")
bookingdate_text = Label(text="موعد تاريخ الحجز المطلوب * ")
firstname_text.place(x=540, y=70)
secondname_text.place(x=540, y=130)
thirdname_text.place(x=540, y=190)
age_text.place(x=540, y=250)
phonenumber_text.place(x=540, y=310)
Departmentname_text.place(x=540, y=370)
familymembers_text.place(x=540, y=430)
bookingdate_text.place(x=540, y=490)
firstname = StringVar()
secondname = StringVar()
thirdname = StringVar()
age = StringVar()
phonenumber = StringVar()
Departmentname = StringVar()
familymembers = StringVar()
bookingdate = StringVar()
firstname_entry = Entry(textvariable=firstname, width="40")
secondname_entry = Entry(textvariable=secondname, width="40")
thirdname_entry = Entry(textvariable=thirdname, width="40")
age_entry = Entry(textvariable=age, width="40")
phonenumber_entry = Entry(textvariable=phonenumber, width="40")
Departmentname_entry = Entry(textvariable=Departmentname, width="40")
familymembers_entry = Entry(textvariable=familymembers, width="40")
bookingdate_entry = Entry(textvariable=bookingdate, width="40")
firstname_entry.place(x=450, y=100)
secondname_entry.place(x=450, y=160)
thirdname_entry.place(x=450, y=220)
age_entry.place(x=450, y=280)
phonenumber_entry.place(x=450, y=340)
Departmentname_entry.place(x=450, y=400)
familymembers_entry.place(x=450, y=460)
bookingdate_entry.place(x= 450, y=520)
register = Button(screen,text="Register", width="60", height="2", command=send_info, bg="grey")
register.place(x=360, y=600)
screen.mainloop()
Result:
This is because Tkinter doesn't allow to use more than one placement of a widget in the whole script. So, if you want to run your code, then you'll have to remove either the grid() function everywhere and replace it with the pack() function or the remove the pack() function and replace it with the grid() function.

Can't multiply sequence by non-int of type 'list' with user selected list item

When I do my button click it keeps throwing the error
Can't multiply sequence by non-int of type 'list'
I am unsure what to do to fix this. I have tried to change Frequnit to an int and it doesn't work. I am unsure of the syntax to correct this.
Frequnit is used for user selection of just one unit from the list
#freecodecamp.org
import math
from tkinter import *
root = Tk()
Frequnit = {
'Hz' : (10**0),
'KHz' : (10**3),
'MHz' : (10**6),
'GHz' : (10**9),
'THz' : (10**12),
}
#creating entry
f1 = Entry (root)
d1 = Entry (root, width= 4)
Gtx1 = Entry (root, width= 4)
Grx1 = Entry (root, width= 4)
MYSUT_TX1 = Entry (root, width= 5)
f1.grid (row=1, column=1)
d1.grid (row=2, column=1)
Gtx1.grid (row=3, column=1)
Grx1.grid (row=4, column=1)
MYSUT_TX1.grid (row=8, column=1)
def myClick():
#variables
c = (2.99 * (10**8))
d = float(d1.get())
f = (float(f1.get())*(Frequnit[]))
Gtx = float(Gtx1.get())
Grx = float(Grx1.get())
FSPL_cal1 = round(abs((20 * math.log10(d)) + (20 * math.log10(f)) + (20 * math.log10( (4 * math.pi ) / c ))) - Gtx - Grx,2)
FSPL_cal2 = round(abs((20 * math.log10(d)) + (20 * math.log10(f)) + (20 * math.log10( (4 * math.pi ) / c ))) - Gtx - Grx +6,2)
#Output1 GUI
FSPL = Label(root, text="FSPL ONE-WAY")
FSPL2 = Label(root, text="FSPL TWO-WAY")
FSPL_cal1 = Label(root, text=FSPL_cal1 )
FSPL_cal2 = Label(root, text=FSPL_cal2)
FSPL.grid (row=6, column=0)
FSPL_cal1.grid (row=6, column=1)
FSPL2.grid (row=7, column=0)
FSPL_cal2.grid (row=7, column=1)
def myClick2():
#variables
c = (2.99 * (10**8))
d = float(d1.get())
f = (float(f1.get())*(10**6))
Gtx = float(Gtx1.get())
Grx = float(Grx1.get())
FSPL_cal1 = round(abs((20 * math.log10(d)) + (20 * math.log10(f)) + (20 * math.log10( (4 * math.pi ) / c ))) - Gtx - Grx,2)
FSPL_cal2 = round(abs((20 * math.log10(d)) + (20 * math.log10(f)) + (20 * math.log10( (4 * math.pi ) / c ))) - Gtx - Grx +6,2)
MYSUT_TX = float(MYSUT_TX1.get())
FSPL_cal1 = round(abs((20 * math.log10(d)) + (20 * math.log10(f)) + (20 * math.log10( (4 * math.pi ) / c ))) - Gtx - Grx,2)
TargetSUT_RX = round((MYSUT_TX - FSPL_cal1),2)
Target_TX = (TargetSUT_RX -6)
MYSUT_TX = Label(root, text=TargetSUT_RX )
Target_TX = Label(root, text=Target_TX )
MYSUT_TX.grid (row=10, column=1)
Target_TX.grid (row=11, column=1)
#creating label widget
Title = Label( root, text="Free Space Loss Calculator")
Frequency = Label(root, text="Frequency")
Distance = Label(root, text="Distance")
Tx_Gain = Label(root, text="Transmit Gain")
Rx_Gain = Label(root, text="Recieve Gain")
FSPL = Button(root, text="Free Space Path loss Calculation", command=myClick)
MYSUT_TX= Label(root, text="SUT Transmit Power")
TargetPW = Button(root, text="Power Recieved at Target", command=myClick2)
MYSUT_RX= Label(root, text="Power Recieved at target")
Target_TX= Label(root, text="Return Power from target")
Tx_Gain1 = Label(root, text="dB")
Rx_Gain1 = Label(root, text="dB")
FSPL_1WAY = Label(root, text="dB")
FSPL_2WAY = Label(root, text="dB")
#drop down items
FrequencyUnits = StringVar()#dBm, uWatts, mWatts, Watts, KWatts, MWatts)
DistanceUnits = StringVar()
PowerUnits = StringVar()
FrequencyUnits.set ('Hz')
DistanceUnits.set ("Meters")
PowerUnits.set ("dBm")
# dBm = input()
# uWatts = 10* math.log10(1000*input()) * 10**-6
# mWatts = 10* math.log10(1000*input()) * 10**-3
# Watts = 10* math.log10(1000*input())
# KWatts = 10* math.log10(1000*input()) * 10**3
# MWatts = 10* math.log10(1000*input()) * 10**6
dropFreq = OptionMenu (root, FrequencyUnits, "Hz", "KHz", "MHz", "GHz", "THZ")
dropDist = OptionMenu (root, DistanceUnits, "Inches", "Feet", "Yards","Meters", "Miles", "NMI")
droppower = OptionMenu (root, PowerUnits, "dBm", "uWatts", "mWatts","Watts", "KWatts", "MWatts")
dropPRT = OptionMenu (root, PowerUnits, "dBm", "uWatts", "mWatts","Watts", "KWatts", "MWatts")
dropRPFT = OptionMenu (root, PowerUnits, "dBm", "uWatts", "mWatts","Watts", "KWatts", "MWatts")
#GUI lay out
Title.grid (row=0, column=1)
Frequency.grid (row=1, column=0)
Distance.grid (row=2, column=0)
Tx_Gain.grid (row=3, column=0)
Rx_Gain.grid (row=4, column=0)
FSPL.grid (row=5, column=1)
MYSUT_TX.grid (row=8, column=0)
TargetPW.grid (row=9, column=1)
MYSUT_RX.grid (row=10, column=0)
Target_TX.grid (row=11, column=0)
dropFreq.grid (row=1, column=2)
dropDist.grid (row=2, column=2)
Tx_Gain1.grid (row=3, column=2)
Rx_Gain1.grid (row=4, column=2)
FSPL_1WAY.grid (row=6, column=2)
FSPL_2WAY.grid (row=7, column=2)
FSPL_2WAY.grid (row=7, column=2)
droppower.grid (row=8, column=2)
dropPRT.grid (row=10, column=2)
dropRPFT.grid (row=11, column=2)
root.mainloop()
You have 2 entry boxes, so my guess is that 1 is for a number entry and the 2nd is for a frequency entry.
f1 = number entry
d1 = frequency entry
If so I would code it as follows:
def myClick():
for key in Frequnit.keys():
if key == d1.get():
result = float(f1.get()) * Frequnit[key]
resultLabel = Label(root, text=str(result)+' '+key)
resultLabel.pack()
You can't turn a whole dictionary to a number, but you can access the values by referencing the key. Saying Frequnit['Hz'] is the same as saying (10**0).
I used the for and if statements for entry validation. You can get rid of those 2 lines and use Frequnit[d1.get()] and get the same result (assuming d1 is found in the dict keys).

Python/Tkinter -

I am developing an application to calculate the billing tax, and at the same time, if the value of the calculation base ((receita1 * 32/100) + (receita2 * 8/100)) is greater than 60k, it calculates the surplus of that, (((receita1 * 32/100) + (receita2 * 8/100)) - 60000), however is giving the following error:
v_result3.set(real(receita1 * 32 / 100) + (receita2 * 8 / 100))
TypeError: can only concatenate str (not "float") to str
Here is the complete code:
from tkinter import *
root = Tk()
root.geometry('350x350')
def real(my_value):
a = '{:,.2f}'.format(float(my_value))
b = a.replace(',', 'v')
c = b.replace('.', ',')
return c.replace('v', '.')
l_label = Label(root, text='Receita 1')
l_label.place(x=10, y=10)
e_entry = Entry(root)
e_entry.place(x=100, y=10)
l_label2 = Label(root, text='Receita 2')
l_label2.place(x=10, y=40)
e_entry2 = Entry(root)
e_entry2.place(x=100, y=40)
v_result = DoubleVar()
l_result = Label(root, textvariable=v_result)
l_result.place(x=10, y=70)
l_explic = Label(root, text='<-- receita1 x 32%')
l_explic.place(x=100, y=70)
v_result2 = DoubleVar()
l_result2 = Label(root, textvariable=v_result2)
l_result2.place(x=10, y=100)
l_explic2 = Label(root, text='<-- receita2 x 8%')
l_explic2.place(x=100, y=100)
v_result3 = DoubleVar()
l_result3 = Label(root, textvariable=v_result3)
l_result3.place(x=10, y=130)
l_explic3 = Label(root, text='<-- receita1 x 32% + receita2 x 8%')
l_explic3.place(x=100, y=130)
v_result4 = DoubleVar()
l_result4 = Label(root, textvariable=v_result4)
l_result4.place(x=10, y=160)
l_explic4 = Label(root, text='<-- result if')
l_explic4.place(x=100, y=160)
def calc():
receita1 = int(e_entry.get())
receita2 = int(e_entry2.get())
v_result.set(real(receita1 * 32 / 100))
v_result2.set(real(receita2 * 8 / 100))
v_result3.set(real(receita1 * 32 / 100) + (receita2 * 8 / 100))
if v_result3.get() > 60000:
v_result4.set(real((receita1 * 32 / 100) + (receita2 * 8 / 100)) - 60000)
elif v_result3.get() < 60000:
v_result4.set(real(receita1 * 32 / 100) + (receita2 * 8 / 100))
e_entry.delete(0, END)
e_entry2.delete(0, END)
bt = Button(root, text='calc', command=calc)
bt.place(x=10, y=200)
root.mainloop()
You're seeing this problem because you're mixing and matching numbers formatted as strings and numbers themselves.
It's better to form your computation into input / computation / output. I've also taken the liberty to use Decimal numbers instead of plain integers, since you seem to be dealing with money, and precision is generally tantamount in that domain.
from decimal import Decimal
from tkinter import *
root = Tk()
root.geometry("350x350")
l_label = Label(root, text="Receita 1")
l_label.place(x=10, y=10)
e_entry = Entry(root)
e_entry.place(x=100, y=10)
l_label2 = Label(root, text="Receita 2")
l_label2.place(x=10, y=40)
e_entry2 = Entry(root)
e_entry2.place(x=100, y=40)
v_result1 = StringVar()
l_result1 = Label(root, textvariable=v_result1)
l_result1.place(x=10, y=70)
l_explic1 = Label(root, text="<-- receita1 x 32%")
l_explic1.place(x=100, y=70)
v_result2 = StringVar()
l_result2 = Label(root, textvariable=v_result2)
l_result2.place(x=10, y=100)
l_explic2 = Label(root, text="<-- receita2 x 8%")
l_explic2.place(x=100, y=100)
v_result3 = StringVar()
l_result3 = Label(root, textvariable=v_result3)
l_result3.place(x=10, y=130)
l_explic3 = Label(root, text="<-- receita1 x 32% + receita2 x 8%")
l_explic3.place(x=100, y=130)
v_result4 = StringVar()
l_result4 = Label(root, textvariable=v_result4)
l_result4.place(x=10, y=160)
l_explic4 = Label(root, text="<-- result if")
l_explic4.place(x=100, y=160)
def real(my_value):
return str(my_value.quantize(Decimal("0.02"))).replace(".", ",")
def calc():
# Get inputs
receita1 = Decimal(int(e_entry.get()))
receita2 = Decimal(int(e_entry2.get()))
# Compute
result1 = receita1 * Decimal("0.32")
result2 = receita2 * Decimal("0.08")
result3 = receita1 + receita2
if result3 > 60000:
result4 = result3 - 60000
else:
result4 = result3
# Output
v_result1.set(real(result1))
v_result2.set(real(result2))
v_result3.set(real(result3))
v_result4.set(real(result4))
e_entry.delete(0, END)
e_entry2.delete(0, END)
bt = Button(root, text="calc", command=calc)
bt.place(x=10, y=200)
root.mainloop()

Tkinter buttons are mixing with eachother

I don't know how to explain it but I have two different pages each with a certain number of buttons as grid
Simple form
Complex form
but when I select one of the radio buttons then select the other one after it gets mixed
Chaos
Here is the code for making the grid
import tkinter as tk
from tkinter import filedialog, Text
import os
import time, sys
from time import sleep
root = tk.Tk()
root.geometry('700x700')
def changecolo(i, j):
if not ((i == 0 and j == 0) or (i == 12 and j == 12)):
if mFrame.grid_slaves(row = i, column = j)[0].cget('bg') == 'black':
mFrame.grid_slaves(row = i, column = j)[0].configure(bg = 'white')
elif mFrame.grid_slaves(row = i, column = j)[0].cget('bg') == 'white':
mFrame.grid_slaves(row = i, column = j)[0].configure(bg = 'grey')
elif mFrame.grid_slaves(row = i, column = j)[0].cget('bg') == 'grey':
mFrame.grid_slaves(row = i, column = j)[0].configure(bg = 'black')
def setalg():
value = 6
def makegrid6():
mFrame.grid_forget()
makegrid66()
def makegrid66():
for i in range(6):
for j in range (6):
if i == 0 and j == 0:
grids6 = tk.Button(mFrame, bg='blue', text = (5 - i) + (5 - j), state = 'disabled', highlightcolor="black", highlightthickness=1, width = 11, height = 5)
grids6.grid(row = i, column = j)
elif i == 5 and j == 5:
grids6 = tk.Button(mFrame, bg='red', text = (5 - i) + (5 - j) , state = 'disabled',highlightcolor="black", highlightthickness=1, width = 11, height = 5)
grids6.grid(row = i, column = j)
elif (((i == 0 or i == 1 or i == 2) and (j == 2)) or (i == 4 and j == 1)):
grids6 = tk.Button(mFrame, bg='white', text = (5 - i) + (5 - j) , highlightcolor="black", highlightthickness=1, width = 11, height = 5, state = 'disabled')
grids6.grid(row = i, column = j)
elif (i == 3 and j == 2):
grids6 = tk.Button(mFrame, bg='grey', text = (5 - i) + (5 - j) , highlightcolor="black", highlightthickness=1, width = 11, height = 5, state = 'disabled')
grids6.grid(row = i, column = j)
else:
grids6 = tk.Button(mFrame, bg='black', text = (5 - i) + (5 - j), fg = 'white', highlightcolor="black", highlightthickness=1, width = 11, height = 5, state = 'disabled')
grids6.grid(row = i, column = j)
return mFrame.grid_slaves, i, j
def makegrid10():
mFrame.grid_forget()
makegrid100()
def makegrid100():
for i in range(13):
for j in range (13):
if i == 0 and j == 0:
grids10 = tk.Button(mFrame, bg='blue', text = (12 - i) + (12 - j), fg = 'white', state = 'disabled', highlightcolor="black", highlightthickness=1, width = 4, height = 2, command=lambda x = i, y = j: changecolo(x, y))
grids10.grid(row = i, column = j)
elif i == 12 and j == 12:
grids10 = tk.Button(mFrame, bg='red', text = (12 - i) + (12 - j), fg = 'white', state = 'disabled',highlightcolor="black", highlightthickness=1, width = 4, height = 2, command=lambda x = i, y = j: changecolo(x, y))
grids10.grid(row = i, column = j)
else:
grids10 = tk.Button(mFrame, bg='black', text = (12 - i) + (12 - j), fg = 'white', highlightcolor="black", highlightthickness=1, width = 4, height = 2, command=lambda x = i, y = j: changecolo(x, y))
grids10.grid(row = i, column = j)
return mFrame.grid_slaves, i, j
def play():
if diffVar.get() == "sim":
butons, l, k = makegrid66()
Greedy(butons, l, k)
else:
butons, l, k = makegrid100()
Greedy(butons, l, k)
def aStar():
print(1000010)
def Greedy(butons, k, l):
print(k, l)
def UCS():
print(99)
mFrame = tk.Frame(root, height = 500, width = 500, bg = "lightgrey")
mFrame.place(x= 10, y = 10)
diffVar = tk.StringVar()
dif = tk.Label(root, text = "Problem Difficulty", bg = "Lightgrey")
dif.place (x = 550, y = 30)
simp = tk.Radiobutton(root, text = "Simple", value = "sim", bg = "Lightgrey", variable = diffVar, command = makegrid6)
simp.place(x = 550, y = 60)
comp = tk.Radiobutton(root, text = "Complex", value = "com", bg = "Lightgrey", variable = diffVar, command = makegrid10)
comp.place(x = 550, y = 90)
algVar = tk.IntVar()
algVar.set(1)
dif = tk.Label(root, text = "Choose an Algorithm", bg = "Lightgrey")
dif.place (x = 550, y = 180)
alg1 = tk.Radiobutton(root, text = "A*", value = 1, bg = "Lightgrey", variable = algVar, command = setalg)
alg1.place(x = 550, y = 210)
alg2 = tk.Radiobutton(root, text = "Greedy", value = 2, bg = "Lightgrey", variable = algVar, command = setalg)
alg2.place(x = 550, y = 240)
alg3 = tk.Radiobutton(root, text = "UCS", value = 3, bg = "Lightgrey", variable = algVar, command = setalg)
alg3.place(x = 550, y = 270)
playbu = tk.Button(root, text = "Play", bg = "White", width = 15, command = play)
playbu.place(x = 10, y = 550)
nextbu = tk.Button(root, text = "Next", bg = "White", width = 15)
nextbu.place(x = 140, y = 550)
fasterbu = tk.Button(root, text = "Faster", bg = "White", width = 15)
fasterbu.place(x = 270, y = 550)
resetbu = tk.Button(root, text = "Reset", bg = "White", width = 15)
resetbu.place(x = 400, y = 550)
root.mainloop()
is their any possible way that the buttons will resize themself in a way that they fit in the same size as the canvas, without me adjusting the width and the height of the button
First I used this to remove old Frame with all buttons and to create new Frame for new buttons
def makegrid6():
global mFrame
mFrame.destroy()
mFrame = tk.Frame(root, height = 500, width = 500, bg = "lightgrey")
mFrame.place(x= 10, y = 10)
makegrid66()
but it had one problem - new Frame was created on top of other widgets so then were hidden and unavaliable.
So I created xFrame in which I put mFrame and xFrame will be all time in window so it will be no moved on top of other widgets
root = tk.Tk()
root.geometry('700x700')
# parent `root`
xFrame = tk.Frame(root, height = 500, width = 500, bg = "lightgrey")
xFrame.place(x= 10, y = 10)
# parent `xFrame`
mFrame = tk.Frame(xFrame, height = 500, width = 500, bg = "lightgrey")
mFrame.pack()
and then it show new frame behind checkbox and other buttons.
To put mFrame in xFrame I use pack() instead of place() because it doesn't neeed options and there will be no other widgets so there is no need to set x,y.
You said that you put mFrame in Canvas (but I don't see it in code) so you could use Canvas instead of xFrame.
BTW: There is suggestion to not use spaces around = inside functions.
See PEP 8 -- Style Guide for Python Code.
Here code with xFrame
I changed also makegrid100() to make it shorter and more readable.
import tkinter as tk
from tkinter import filedialog, Text
import os
import time, sys
from time import sleep
# --- functions ---
def changecolo(i, j):
if not ((i == 0 and j == 0) or (i == 12 and j == 12)):
if mFrame.grid_slaves(row=i, column=j)[0].cget('bg') == 'black':
mFrame.grid_slaves(row=i, column=j)[0].configure(bg='white')
elif mFrame.grid_slaves(row=i, column=j)[0].cget('bg') == 'white':
mFrame.grid_slaves(row=i, column=j)[0].configure(bg='grey')
elif mFrame.grid_slaves(row=i, column=j)[0].cget('bg') == 'grey':
mFrame.grid_slaves(row=i, column=j)[0].configure(bg='black')
def setalg():
value = 6
def makegrid6():
global mFrame # inform function to use external/global variable instead of locla one when it will assign value (`=`)
mFrame.destroy()
mFrame = tk.Frame(xFrame, height=500, width=500, bg="lightgrey")
mFrame.pack()
makegrid66()
def makegrid66():
for i in range(6):
for j in range (6):
if i == 0 and j == 0:
grids6=tk.Button(mFrame, bg='blue', text=(5 - i) + (5 - j), state='disabled', highlightcolor="black", highlightthickness=1, width=11, height=5)
grids6.grid(row=i, column=j)
elif i == 5 and j == 5:
grids6=tk.Button(mFrame, bg='red', text=(5 - i) + (5 - j) , state='disabled', highlightcolor="black", highlightthickness=1, width=11, height=5)
grids6.grid(row=i, column=j)
elif (((i == 0 or i == 1 or i == 2) and (j == 2)) or (i == 4 and j == 1)):
grids6=tk.Button(mFrame, bg='white', text=(5 - i) + (5 - j) , highlightcolor="black", highlightthickness=1, width=11, height=5, state='disabled')
grids6.grid(row=i, column=j)
elif (i == 3 and j == 2):
grids6=tk.Button(mFrame, bg='grey', text=(5 - i) + (5 - j) , highlightcolor="black", highlightthickness=1, width=11, height=5, state='disabled')
grids6.grid(row=i, column=j)
else:
grids6=tk.Button(mFrame, bg='black', text=(5 - i) + (5 - j), fg='white', highlightcolor="black", highlightthickness=1, width=11, height=5, state='disabled')
grids6.grid(row=i, column=j)
return mFrame.grid_slaves, i, j
def makegrid10():
global mFrame # inform function to use external/global variable instead of locla one when it will assign value (`=`)
mFrame.destroy()
mFrame = tk.Frame(xFrame, height=500, width=500, bg="lightgrey")
mFrame.pack()
makegrid100()
def makegrid100():
for i in range(13):
for j in range (13):
if i == 0 and j == 0:
bg_color = 'blue'
state = 'disabled'
elif i == 12 and j == 12:
bg_color = 'red'
state = 'disabled'
else:
bg_color = 'black'
state = 'normal'
b = tk.Button(mFrame, bg=bg_color, text=(12-i)+(12-j), state=state, fg='white', highlightcolor="black", highlightthickness=1, width=4, height=2, command=lambda x=i, y=j: changecolo(x, y))
b.grid(row=i, column=j)
return mFrame.grid_slaves, i, j
def play():
if diffVar.get() == "sim":
butons, l, k=makegrid66()
Greedy(butons, l, k)
else:
butons, l, k=makegrid100()
Greedy(butons, l, k)
def aStar():
print(1000010)
def Greedy(butons, k, l):
for i in range(k + 1):
for j in range(l + 1):
print(i, j)
mini=butons(row=i, column=j)[0].cget('text')
m, n=i, j
if i - 1 > -1:
if butons(row=i - 1, column=j)[0].cget('text') < mini:
mini=butons(row=i - 1, column=j)[0].cget('text')
m=i - 1
if i + 1 < 6:
if butons(row=i + 1, column=j)[0].cget('text') < mini:
mini=butons(row=i + 1, column=j)[0].cget('text')
m=i + 1
if j - 1 > -1:
if butons(row=i, column=j - 1)[0].cget('text') < mini:
mini=butons(row=i, column=j - 1)[0].cget('text')
n=j - 1
if j + 1 < 6:
if butons(row=i, column=j + 1)[0].cget('text') < mini:
mini=butons(row=i, column=j + 1)[0].cget('text')
n=j + 1
i, j=m, n
butons(row=m, column=n)[0].configure(bg='brown')
def UCS():
print(99)
# --- main ---
root = tk.Tk()
root.geometry('700x700')
xFrame = tk.Frame(root, height=500, width=500, bg="lightgrey")
xFrame.place(x= 10, y=10)
mFrame = tk.Frame(xFrame, height=500, width=500, bg="lightgrey")
mFrame.pack()
diffVar = tk.StringVar()
dif = tk.Label(root, text="Problem Difficulty", bg="Lightgrey")
dif.place (x=550, y=30)
simp = tk.Radiobutton(root, text="Simple", value="sim", bg="Lightgrey", variable=diffVar, command=makegrid6)
simp.place(x=550, y=60)
comp = tk.Radiobutton(root, text="Complex", value="com", bg="Lightgrey", variable=diffVar, command=makegrid10)
comp.place(x=550, y=90)
algVar = tk.IntVar()
algVar.set(1)
dif = tk.Label(root, text="Choose an Algorithm", bg="Lightgrey")
dif.place (x=550, y=180)
alg1 = tk.Radiobutton(root, text="A*", value=1, bg="Lightgrey", variable=algVar, command=setalg)
alg1.place(x=550, y=210)
alg2 = tk.Radiobutton(root, text="Greedy", value=2, bg="Lightgrey", variable=algVar, command=setalg)
alg2.place(x=550, y=240)
alg3 = tk.Radiobutton(root, text="UCS", value=3, bg="Lightgrey", variable=algVar, command=setalg)
alg3.place(x=550, y=270)
f2 = tk.Canvas(root, height=30, width=30, bg="White")
playbu = tk.Button(root, text="Play", bg="White", width=15, command=play)
playbu.place(x=10, y=550)
nextbu = tk.Button(root, text="Next", bg="White", width=15)
nextbu.place(x=140, y=550)
fasterbu = tk.Button(root, text="Faster", bg="White", width=15)
fasterbu.place(x=270, y=550)
resetbu = tk.Button(root, text="Reset", bg="White", width=15)
resetbu.place(x=400, y=550)
root.mainloop()

How to connect the Calculator GUI to the graph GUI?

I want to connect calculator gui with function graph gui.
For example
if I clicked 'sinx' on the calculator, the graph(which is sine function) would be apear on graph gui.
How could I do?
Calculator GUI code
from tkinter import *
window = Tk()
window.title("해대비주얼")
''
top_row = Frame(window)
top_row.grid(row=0, column=0, columnspan=2, sticky=N)
display = Entry(top_row, width=35, bg="light blue")
display.grid()
num_pad = Frame(window)
num_pad.grid(row=1, column=0, sticky=W)
num_pad_list = [
'7','8','9',
'4','5','6',
'1','2','3',
'0','.','=']
r = 0
c = 0
for btn_text in num_pad_list:
def cmd(x=btn_text):
click(x)
Button(num_pad, text=btn_text, width=5, command=cmd).grid(row=r, column=c)
c = c + 1
if c > 2:
c = 0
r = r + 1
operator = Frame(window)
operator.grid(row=1, column=1, sticky=E)
operator_list = [
'*','/',
'+','-',
'(',')',
'^','C']
r = 0
c = 0
for btn_text in operator_list:
def cmd(x=btn_text):
click(x)
Button(operator, text=btn_text, width=5, command=cmd).grid(row=r, column=c)
c = c + 1
if c > 1:
c = 0
r = r + 1
etc = Frame(window)
etc.grid(row=50, column=0, sticky=S)
etc_list = ['pi','sin','cos','x']
r = 0
c = 0
for btn_text in etc_list:
def cmd(x=btn_text):
click(x)
Button(etc, text=btn_text, width=5, command=cmd).grid(row=r, column=c)
r = 0
c = c + 1
def click(key):
if key == "=":
try:
if "^" in display.get():
n = display.get().split(sep="^")
result = str(float(n[0]) ** float(n[1]))
display.insert(END, " = " + result)
else:
result = str(eval(display.get()))[0:10]
display.insert(END, " = " + result)
except:
display.insert(END, " --> Error!")
elif key == "C":
display.delete(0, END)
elif key == etc_list[0]:
import math
display.insert(END, math.pi)
else:
display.insert(END, key)
window.mainloop()
2.Graph GUI code
from tkinter import *
graph = Tk()
graph.title("해대비주얼 그래프")
''
graph.geometry("1000x700")
''

Categories

Resources