python-3.x 如何使用tkinter将我的代码转换为选项卡?

mwngjboj  于 2023-01-10  发布在  Python
关注(0)|答案(1)|浏览(139)

我一直在解决这个问题,不能使它工作的方式我想要的,我是完全新的tkinter和我试图解决任何我可以同时编码这整个事情对我自己.这里的问题是,当我尝试使用ttk.notebook到我的当前代码这是主窗口计算,我想创建一个标签为每个计算器为我自己的迷你项目.

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

5uzkadbs1#

笔记本允许您将框架作为选项卡添加到其中,因此,如果您将“计算器”设置为框架,则只需将其添加到笔记本的某个选项卡即可。
注意,通常建议只控制比当前帧深一级的子级,而您在truepositioncal中控制两级的子级,这是不建议的,并且会使代码更难阅读。
改变你的计算器到一个框架是这推荐的方法并且手工地添加它到这主窗口笔记本是如下.

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

如果你想把额外的逻辑包含在一个类中,那么你应该把它包含在另一个类中(称之为MainWindow),但是不要把那个逻辑放在你的计算器中,这样你的计算器就保持了一个独立的可重用框架,你可以在任何地方添加/重用它。

相关问题