英文:
Align input labels and results using Tkinter (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.
import tkinter as tk
import math
from tkinter import *
def peso_proprio(gama_c, bm, H, hb, B, phi, gama_s):
# Your function code here
def calculate():
# Your calculation code here
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="VARIÁVEIS DE ENTRADA")
variaveis_label.pack()
gama_c_label = tk.Label(root, text="Entre γ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()
# Create a PhotoImage object
image = PhotoImage(file="C:/Users/Usuário/Desktop/Muro_Arrimo.png")
# Create a label to display the image
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)
Please note that I've removed the HTML entities and formatted the code in plain text as requested.
英文:
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.
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)
答案1
得分: 1
使用tk.Frame会使布局更容易。抱歉,我的英文不太好,所以很难解释。
import tkinter as tk
import math
from tkinter import *
def peso_proprio(gama_c, bm, H, hb, B, phi, gama_s):
# 这里是计算函数,不做翻译
# ...
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():
# 这里是计算函数,不做翻译
# ...
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()
# 这里是其他标签和输入框,不做翻译
calculate_button = tk.Button(input_frame, text="CALCULAR", command=calculate)
calculate_button.pack()
result_label = tk.Label(output_frame, text="")
result_label.pack()
# 创建一个PhotoImage对象
image = PhotoImage(file="C:/Users/Usuário/Desktop/Muro_Arrimo.png")
# 创建用于显示图像的标签
image_label = tk.Label(pic_frame, image=image)
image_label.pack()
root.mainloop()
以上是代码的主要部分,我已经将注释中的内容保留在原文中,不做翻译。
英文:
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()
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论