英文:
how to convert my code into a tab using tkinter?
问题
以下是您提供的代码的翻译:
import tkinter as tk
from tkinter import ttk
import math as mt
class truepositioncal:
def __init__(self):
# 创建主窗口
self.main_window = tk.Tk()
# 主窗口标题
self.main_window.title('True Position Calculator')
# 主窗口大小
self.main_window.geometry('400x300')
# 标签页小部件
self.tab_control = ttk.Notebook(self.main_window)
self.tab_control2 = ttk.Notebook(self.main_window)
# 创建框架
self.maxtrueposition_frame = tk.Frame(self.tab_control)
self.nom_x_frame = tk.Frame(self.tab_control)
self.nom_y_frame = tk.Frame(self.tab_control)
self.act_x_frame = tk.Frame(self.tab_control)
self.act_y_frame = tk.Frame(self.tab_control)
self.nom_dia_frame = tk.Frame(self.tab_control)
self.tol_pos_frame = tk.Frame(self.tab_control)
self.tol_neg_frame = tk.Frame(self.tab_control)
self.act_dia_frame = tk.Frame(self.tab_control)
self.result_frame = tk.Frame(self.tab_control)
self.calculate_frame = tk.Frame(self.tab_control)
self.mmc_frame = tk.Frame(self.tab_control)
self.lmc_frame = tk.Frame(self.tab_control)
# 添加标签页
self.tab_control.add(self.maxtrueposition_frame, text='标签-0')
self.tab_control.add(self.nom_x_frame, text='标签-0')
self.tab_control.add(self.nom_y_frame, text='标签-0')
self.tab_control.add(self.act_x_frame, text='标签-0')
self.tab_control.add(self.act_y_frame, text='标签-0')
self.tab_control.add(self.nom_dia_frame, text='标签-0')
self.tab_control.add(self.tol_pos_frame, text='标签-0')
self.tab_control.add(self.tol_neg_frame, text='标签-0')
self.tab_control.add(self.act_dia_frame, text='标签-0')
self.tab_control.add(self.result_frame, text='标签-0')
self.tab_control.add(self.calculate_frame, text='标签-0')
self.tab_control.add(self.mmc_frame, text='标签-0')
self.tab_control.add(self.lmc_frame, text='标签-0')
self.tab_control.pack(fill=tk.BOTH, expand=True)
self.tab_control2.pack(fill=tk.BOTH, expand=True)
# 最大真实位置的提示标签
self.promptlabelmaxpos = tk.Label(self.maxtrueposition_frame, text='最大位置偏差:')
# 输入最大真实位置
self.maxposdev = tk.Entry(self.maxtrueposition_frame, width=10)
self.promptlabelmaxpos.pack(side='left')
self.maxposdev.pack(side='left')
# 提示标签标称X值
self.labeldrawingpara_x = tk.Label(self.nom_x_frame, text='标称X值:')
self.nominal_x = tk.Entry(self.nom_x_frame, width=10)
# 左侧对齐(标称X)
self.labeldrawingpara_x.pack(side='left')
self.nominal_x.pack(side='left')
# 提示标签标称Y值
self.labeldrawingpara_y = tk.Label(self.nom_y_frame, text='标称Y值:')
self.nominal_y = tk.Entry(self.nom_y_frame, width=10)
self.labeldrawingpara_y.pack(side='left')
self.nominal_y.pack(side='left')
# 提示标签实际X值
self.labelact_x = tk.Label(self.act_x_frame, text='实际X值:')
self.act_x = tk.Entry(self.act_x_frame, width=10)
self.labelact_x.pack(side='left')
self.act_x.pack(side='left')
# 提示标签实际Y值
self.labelact_y = tk.Label(self.act_y_frame, text='实际Y值:')
self.act_y = tk.Entry(self.act_y_frame, width=10)
self.labelact_y.pack(side='left')
self.act_y.pack(side='left')
# 提示标签标称直径
self.labelnomdia = tk.Label(self.nom_dia_frame, text='标称直径值:')
self.nom_dia = tk.Entry(self.nom_dia_frame, width=10)
self.labelnomdia.pack(side='left')
self.nom_dia.pack(side='left')
# 提示标签公差正向
self.labeltolpos = tk.Label(self.tol_pos_frame, text='公差(+)值:')
self.tol_pos = tk.Entry(self.tol_pos_frame, width=10)
self.labeltolpos.pack(side='left')
self.tol_pos.pack(side='left')
# 提示标签公差负向
self.labeltolneg = tk.Label(self.tol_neg_frame, text='公差(-)值:')
self.tol_neg = tk.Entry(self.tol_neg_frame, width=10)
self.labeltolneg.pack(side='left')
self.tol_neg.pack(side='left')
# 实际直径的提示标签
self.labelactdia = tk.Label(self.act_dia_frame, text='实际直径:')
self.act_dia = tk.Entry(self.act_dia_frame, width=10)
self.labelactdia.pack(side='left')
self.act_dia.pack(side='left')
# 创建并打包真实位置结果的小部件
self.result_label = tk.Label(self.result_frame, text='实际位置结果:')
self.rs = tk.StringVar()
self.rs_label = tk.Label(self.result_frame, textvariable=self.rs)
self.result_label.pack(side='left')
self.rs_label.pack(side='left')
self.calc_button = tk.Button(self.calculate_frame, text='计算',
command=self.caltruepos)
self.calc_button.pack(side='left')
# 创建MMC结果的小部件
self.mmc_result_label = tk.Label(self.mmc_frame, text='允许的位置公差(MMC):')
self.mmc = tk.StringVar()
self.mmc_label = tk.Label(self.mmc_frame, textvariable=self.mmc)
self.mmc_result_label.pack(side='left')
self.mmc_label.pack(side='left')
# 创建LMC结果的小部件
self.lmc_result_label = tk.Label(self.lmc_frame, text='允许的位置公差(LMC):')
self.lmc = tk.StringVar()
self.lmc_label = tk.Label(self.lmc_frame, textvariable=self.lmc)
self.lmc_result_label.pack(side='left')
self.lmc_label.pack(side='left')
# 打包到一起
self.maxtrueposition_frame.pack()
<details>
<summary>英文:</summary>
i have been solving the problem and couldn't make it to work the way i want, i'm totally new to tkinter and im trying to solve whatever i can while coding this whole thing on my own. the problem here is when i tried using ttk.notebook into my current code which is the main window to calculate, i would like to create a tab for each calculator for my own mini projects.
import tkinter as tk
from tkinter import ttk
import math as mt
class truepositioncal:
def init(self):
#create main window
self.main_window = tk.Tk()
#Main Window Title
self.main_window.title('True Position Calculator')
#main window size
self.main_window.geometry('400x300')
#tabs widgets
self.tab_control = ttk.Notebook(self.main_window)
self.tab_control2 = ttk.Notebook(self.main_window)
#Create Frame
self.maxtrueposition_frame = tk.Frame(self.tab_control)
self.nom_x_frame = tk.Frame(self.tab_control)
self.nom_y_frame = tk.Frame(self.tab_control)
self.act_x_frame = tk.Frame(self.tab_control)
self.act_y_frame = tk.Frame(self.tab_control)
self.nom_dia_frame = tk.Frame(self.tab_control)
self.tol_pos_frame = tk.Frame(self.tab_control)
self.tol_neg_frame = tk.Frame(self.tab_control)
self.act_dia_frame = tk.Frame(self.tab_control)
self.result_frame = tk.Frame(self.tab_control)
self.calculate_frame = tk.Frame(self.tab_control)
self.mmc_frame = tk.Frame(self.tab_control)
self.lmc_frame = tk.Frame(self.tab_control)
#adding tabs
self.tab_control.add(self.maxtrueposition_frame, text ='Tab-0')
self.tab_control.add(self.nom_x_frame, text ='Tab-0')
self.tab_control.add(self.nom_y_frame, text ='Tab-0')
self.tab_control.add(self.act_x_frame, text ='Tab-0')
self.tab_control.add(self.act_y_frame, text ='Tab-0')
self.tab_control.add(self.nom_dia_frame, text ='Tab-0')
self.tab_control.add(self.tol_pos_frame, text ='Tab-0')
self.tab_control.add(self.tol_neg_frame, text ='Tab-0')
self.tab_control.add(self.act_dia_frame, text ='Tab-0')
self.tab_control.add(self.result_frame, text ='Tab-0')
self.tab_control.add(self.calculate_frame, text ='Tab-0')
self.tab_control.add(self.mmc_frame, text ='Tab-0')
self.tab_control.add(self.lmc_frame, text ='Tab-0')
self.tab_control.pack(fill= tk.BOTH, expand=True)
self.tab_control2.pack(fill= tk.BOTH, expand=True)
#prompt label Max True position
self.promptlabelmaxpos = tk.Label(self.maxtrueposition_frame, text = 'Max Position Deviation:')
#Input Max True Position
self.maxposdev = tk.Entry(self.maxtrueposition_frame, width = 10)
self.promptlabelmaxpos.pack(side = 'left')
self.maxposdev.pack(side = 'left')
#Prompt label nominal X
self.labeldrawingpara_x = tk.Label(self.nom_x_frame, text = 'Nominal X Value:')
self.nominal_x = tk.Entry(self.nom_x_frame, width=10)
#pack to left (nominal X)
self.labeldrawingpara_x.pack(side = 'left')
self.nominal_x.pack(side = 'left')
#Prompt label nominal y
self.labeldrawingpara_y = tk.Label(self.nom_y_frame, text = 'Nominal Y Value:')
self.nominal_y = tk.Entry(self.nom_y_frame, width=10)
self.labeldrawingpara_y.pack(side = 'left')
self.nominal_y.pack(side = 'left')
#Prompt label actual X
self.labelact_x = tk.Label(self.act_x_frame, text = 'Actual X Value:')
self.act_x = tk.Entry(self.act_x_frame, width=10)
self.labelact_x.pack(side = 'left')
self.act_x.pack(side = 'left')
#Prompt label Actual Y
self.labelact_y = tk.Label(self.act_y_frame, text = 'Actual Y Value:')
self.act_y = tk.Entry(self.act_y_frame, width=10)
self.labelact_y.pack(side = 'left')
self.act_y.pack(side = 'left')
#Prompt label Nominal Diameter
self.labelnomdia = tk.Label(self.nom_dia_frame, text = 'Nominal Diameter Value:')
self.nom_dia = tk.Entry(self.nom_dia_frame, width=10)
self.labelnomdia.pack(side = 'left')
self.nom_dia.pack(side = 'left')
#Prompt label tolerance positive
self.labeltolpos = tk.Label(self.tol_pos_frame, text = 'Tolerance (+) Value:')
self.tol_pos = tk.Entry(self.tol_pos_frame, width=10)
self.labeltolpos.pack(side = 'left')
self.tol_pos.pack(side = 'left')
#Prompt label tolerance Negative
self.labeltolneg = tk.Label(self.tol_neg_frame, text = 'Tolerance (-) Value:')
self.tol_neg = tk.Entry(self.tol_neg_frame, width=10)
self.labeltolneg.pack(side = 'left')
self.tol_neg.pack(side = 'left')
#Prompt Label for Actual Diameter
self.labelactdia = tk.Label(self.act_dia_frame, text = 'Actual Diameter:')
self.act_dia = tk.Entry(self.act_dia_frame, width=10)
self.labelactdia.pack(side = 'left')
self.act_dia.pack(side = 'left')
#Create and pack the widgets for the true position results
self.result_label = tk.Label(self.result_frame,text='Actual Position Result:')
self.rs = tk.StringVar()
self.rs_label = tk.Label(self.result_frame,
textvariable=self.rs)
self.result_label.pack(side='left')
self.rs_label.pack(side='left')
self.calc_button = tk.Button(self.calculate_frame, text='Calculate',
command=self.caltruepos)
self.calc_button.pack(side='left')
#create widgets for MMC results
self.mmc_result_label = tk.Label(self.mmc_frame, text='Position Tolerances allowed (MMC):')
self.mmc = tk.StringVar()
self.mmc_label = tk.Label(self.mmc_frame, textvariable=self.mmc)
self.mmc_result_label.pack(side='left')
self.mmc_label.pack(side='left')
#create widgets for lmc results
self.lmc_result_label = tk.Label(self.lmc_frame, text='Position Tolerances allowed (LMC):')
self.lmc = tk.StringVar()
self.lmc_label = tk.Label(self.lmc_frame, textvariable=self.lmc)
self.lmc_result_label.pack(side='left')
self.lmc_label.pack(side='left')
#Pack into one
self.maxtrueposition_frame.pack()
self.nom_x_frame.pack()
self.nom_y_frame.pack()
self.act_x_frame.pack()
self.act_y_frame.pack()
self.nom_dia_frame.pack()
self.tol_pos_frame.pack()
self.tol_neg_frame.pack()
self.act_dia_frame.pack()
self.result_frame.pack()
self.mmc_frame.pack()
self.lmc_frame.pack()
self.calculate_frame.pack()
tk.mainloop()
def caltruepos(self):
#Change string into float numbers
maxtrueposition = float(self.maxposdev.get())
nom_x_a = float(self.nominal_x.get())
nom_y_a = float(self.nominal_y.get())
act_x_a = float(self.act_x.get())
act_y_a = float(self.act_y.get())
nom_dia_a = float(self.nom_dia.get())
tol_pos_a = float(self.tol_pos.get())
tol_neg_a = float(self.tol_neg.get())
act_dia_a = float(self.act_dia.get())
#calculate true position
x = nom_x_a - act_x_a
y = nom_y_a - act_y_a
square_Root = mt.sqrt((x*x) + (y*y))
Value = 2 * square_Root
#calculate true position mmc
a_mmc = act_dia_a - (nom_dia_a - tol_neg_a) + maxtrueposition
#calculate true position lmc
a_lmc = (nom_dia_a + tol_pos_a) - act_dia_a + maxtrueposition
#showing pass or fail
if Value <= maxtrueposition:
self.rs_label["background"] = "green"
else:
self.rs_label["background"] = "red"
if Value <= a_mmc:
self.mmc_label["background"] = "green"
else:
self.mmc_label["background"] = "red"
if Value <= a_lmc:
self.lmc_label["background"] = "green"
else:
self.lmc_label["background"] = "red"
#roundup to 3 decimal places
true_position_result = '{:.3f}'.format(Value)
mmc_result = '{:.3f}'.format(a_mmc)
lmc_result = '{:.3f}'.format(a_lmc)
#float to StringVar
self.rs.set(true_position_result)
self.mmc.set(mmc_result)
self.lmc.set(lmc_result)
trueposcal_a = truepositioncal()
</details>
# 答案1
**得分**: 2
一个笔记本允许您将框架添加为选项卡,所以如果您将您的 "计算器 "转换为一个框架,您可以简单地将它添加到笔记本的某个选项卡中。
请注意,通常建议只控制比当前框架深一级的子级,而您在 `truepositioncal` 中控制了两级深度,这不建议,会使您的代码难以阅读。
将您的计算器更改为一个框架是推荐的方法,手动将其添加到主窗口的笔记本如下。
```python
import tkinter as tk
from tkinter import ttk
import math as mt
# ... 省略了上面的代码 ...
# 创建主窗口类
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
# 主窗口标题
self.title('True Position Calculator')
# 主窗口大小
self.geometry('400x600')
# 创建笔记本
tab_control = ttk.Notebook(self)
tab_control.pack(fill=tk.BOTH, expand=True)
# 创建选项卡并添加到笔记本
trueposcal_a = truepositioncal(tab_control)
tab_control.add(trueposcal_a, text='Tab-0')
trueposcal_b = truepositioncal(tab_control)
tab_control.add(trueposcal_b, text='Tab-1')
# 创建第二个笔记本
tab_control2 = ttk.Notebook(self)
tab_control2.pack(fill=tk.BOTH, expand=True)
# 创建第二个笔记本中的选项卡
trueposcal_a2 = truepositioncal(tab_control2)
tab_control2.add(trueposcal_a2, text='Tab-0')
trueposcal_b2 = truepositioncal(tab_control2)
tab_control2.add(trueposcal_b2, text='Tab-1')
if __name__ == '__main__':
main_window = MainWindow()
main_window.mainloop()
如果您想要将额外的逻辑包含在一个类中,那么您应该将它包含在另一个类中(称之为 MainWindow),但不要将该逻辑放在您的计算器中,这样您的计算器就保持独立的可重复使用的框架,可以在任何地方添加/重复使用。
英文:
A notebook allows you to add frames to it as tabs, so if you make your "calculator" into a frame, you can simply add it to the notebook at a certain tab.
note that it's generally recommended to only control children one level deeper than your current frame, and you are controlling children two levels deep in your truepositioncal
which is not recommended and makes your code harder to read.
changing your calculator into a frame is the recommended way and manually adding it to the main window notebook is as follows.
import tkinter as tk
from tkinter import ttk
import math as mt
class truepositioncal(tk.Frame):
def __init__(self, master=None):
super().__init__(master=master)
# Create Frame
self.maxtrueposition_frame = tk.Frame(self)
self.nom_x_frame = tk.Frame(self)
self.nom_y_frame = tk.Frame(self)
self.act_x_frame = tk.Frame(self)
self.act_y_frame = tk.Frame(self)
self.nom_dia_frame = tk.Frame(self)
self.tol_pos_frame = tk.Frame(self)
self.tol_neg_frame = tk.Frame(self)
self.act_dia_frame = tk.Frame(self)
self.result_frame = tk.Frame(self)
self.calculate_frame = tk.Frame(self)
self.mmc_frame = tk.Frame(self)
self.lmc_frame = tk.Frame(self)
# prompt label Max True position
self.promptlabelmaxpos = tk.Label(self.maxtrueposition_frame, text='Max Position Deviation:')
# Input Max True Position
self.maxposdev = tk.Entry(self.maxtrueposition_frame, width=10)
self.promptlabelmaxpos.pack(side='left')
self.maxposdev.pack(side='left')
# Prompt label nominal X
self.labeldrawingpara_x = tk.Label(self.nom_x_frame, text='Nominal X Value:')
self.nominal_x = tk.Entry(self.nom_x_frame, width=10)
# pack to left (nominal X)
self.labeldrawingpara_x.pack(side='left')
self.nominal_x.pack(side='left')
# Prompt label nominal y
self.labeldrawingpara_y = tk.Label(self.nom_y_frame, text='Nominal Y Value:')
self.nominal_y = tk.Entry(self.nom_y_frame, width=10)
self.labeldrawingpara_y.pack(side='left')
self.nominal_y.pack(side='left')
# Prompt label actual X
self.labelact_x = tk.Label(self.act_x_frame, text='Actual X Value:')
self.act_x = tk.Entry(self.act_x_frame, width=10)
self.labelact_x.pack(side='left')
self.act_x.pack(side='left')
# Prompt label Actual Y
self.labelact_y = tk.Label(self.act_y_frame, text='Actual Y Value:')
self.act_y = tk.Entry(self.act_y_frame, width=10)
self.labelact_y.pack(side='left')
self.act_y.pack(side='left')
# Prompt label Nominal Diameter
self.labelnomdia = tk.Label(self.nom_dia_frame, text='Nominal Diameter Value:')
self.nom_dia = tk.Entry(self.nom_dia_frame, width=10)
self.labelnomdia.pack(side='left')
self.nom_dia.pack(side='left')
# Prompt label tolerance positive
self.labeltolpos = tk.Label(self.tol_pos_frame, text='Tolerance (+) Value:')
self.tol_pos = tk.Entry(self.tol_pos_frame, width=10)
self.labeltolpos.pack(side='left')
self.tol_pos.pack(side='left')
# Prompt label tolerance Negative
self.labeltolneg = tk.Label(self.tol_neg_frame, text='Tolerance (-) Value:')
self.tol_neg = tk.Entry(self.tol_neg_frame, width=10)
self.labeltolneg.pack(side='left')
self.tol_neg.pack(side='left')
# Prompt Label for Actual Diameter
self.labelactdia = tk.Label(self.act_dia_frame, text='Actual Diameter:')
self.act_dia = tk.Entry(self.act_dia_frame, width=10)
self.labelactdia.pack(side='left')
self.act_dia.pack(side='left')
# Create and pack the widgets for the true position results
self.result_label = tk.Label(self.result_frame, text='Actual Position Result:')
self.rs = tk.StringVar()
self.rs_label = tk.Label(self.result_frame,
textvariable=self.rs)
self.result_label.pack(side='left')
self.rs_label.pack(side='left')
self.calc_button = tk.Button(self.calculate_frame, text='Calculate',
command=self.caltruepos)
self.calc_button.pack(side='left')
# create widgets for MMC results
self.mmc_result_label = tk.Label(self.mmc_frame, text='Position Tolerances allowed (MMC):')
self.mmc = tk.StringVar()
self.mmc_label = tk.Label(self.mmc_frame, textvariable=self.mmc)
self.mmc_result_label.pack(side='left')
self.mmc_label.pack(side='left')
# create widgets for lmc results
self.lmc_result_label = tk.Label(self.lmc_frame, text='Position Tolerances allowed (LMC):')
self.lmc = tk.StringVar()
self.lmc_label = tk.Label(self.lmc_frame, textvariable=self.lmc)
self.lmc_result_label.pack(side='left')
self.lmc_label.pack(side='left')
# Pack into one
self.maxtrueposition_frame.pack()
self.nom_x_frame.pack()
self.nom_y_frame.pack()
self.act_x_frame.pack()
self.act_y_frame.pack()
self.nom_dia_frame.pack()
self.tol_pos_frame.pack()
self.tol_neg_frame.pack()
self.act_dia_frame.pack()
self.result_frame.pack()
self.mmc_frame.pack()
self.lmc_frame.pack()
self.calculate_frame.pack()
def caltruepos(self):
... # add your logic here
main_window = tk.Tk()
# Main Window Title
main_window.title('True Position Calculator')
# main window size
main_window.geometry('400x600')
# tab notebook
tab_control = ttk.Notebook(main_window)
tab_control.pack(fill=tk.BOTH, expand=True)
# tabs widgets
trueposcal_a = truepositioncal(tab_control)
tab_control.add(trueposcal_a, text='Tab-0')
trueposcal_b = truepositioncal(tab_control)
tab_control.add(trueposcal_b, text='Tab-1')
# tab notebook 2
tab_control2 = ttk.Notebook(main_window)
tab_control2.pack(fill=tk.BOTH, expand=True)
# tabs widgets 2
trueposcal_a2 = truepositioncal(tab_control2)
tab_control2.add(trueposcal_a2, text='Tab-0')
trueposcal_b2 = truepositioncal(tab_control2)
tab_control2.add(trueposcal_b2, text='Tab-1')
tk.mainloop()
if you want the extra logic to be contained in a class then you should contain it in another class (call it MainWindow), but don't put that logic inside you calculator, so your calculator stays an independent reusable frame, that you can add/reuse anywhere.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论