Python-你能帮我从一段代码I';我在重复?

Python-你能帮我从一段代码I';我在重复?,python,class,tkinter,Python,Class,Tkinter,下面我在设置instruct1和instruct2变量时重用了一段代码。昨天我试了几件事来上课,但都没能做好。有人愿意帮忙吗?提前谢谢 import tkinter as tk from tkinter import ttk HEIGHT = 700 WIDTH = 1100 root = tk.Tk() canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH) canvas.pack() frame = tk.Frame(root, bg

下面我在设置instruct1和instruct2变量时重用了一段代码。昨天我试了几件事来上课,但都没能做好。有人愿意帮忙吗?提前谢谢

import tkinter as tk
from tkinter import ttk

HEIGHT = 700
WIDTH = 1100

root = tk.Tk()

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

frame = tk.Frame(root, bg='#08295e')
frame.place(relx=0.01, rely=0.01, relwidth=0.98, relheight=0.98)

prog_title = tk.Label(frame, text='test program')
prog_title.config(width=30, font=('Eras Medium ITC', 30), bg='#08295e', fg='white', bd=7)
prog_title.pack()

instruct1 = tk.Label(frame, text='Step1: Do step 1', anchor='nw')
instruct1.config(width=30, font=('Microsoft JhengHei Light', 15), bg='#08295e', fg='white')
instruct1.place(relx=0.05, rely=0.1)

instruct2 = tk.Label(frame, text='Step2: Do step 2', anchor='nw')
instruct2.config(width=30, font=('Microsoft JhengHei Light', 15), bg='#08295e', fg='white')
instruct2.place(relx=0.05, rely=0.2)

s = ttk.Style()
s.configure('my.TButton', font=('Microsoft JhengHei Light', 12))

button = ttk.Button(root, text='Create Stuff', style='my.TButton')
button.place(relx=0.5, rely=0.3, relwidth=0.17, relheight=0.05)

root.mainloop()

只要看一下,这两行之间哪些值是常量,将它们包装在函数中,并将其他值定义为参数

def make_label(my_text, my_offset):
      temp = tk.Label(frame, text=my_text, anchor='nw')
      temp.config(width=30, font=('Microsoft JhengHei Light', 15), bg='#08295e', fg='white')
      temp.place(relx=0.05, rely=my_offset)
      return temp

instruct1 = make_label('Step1: Do step 1', 0.1)       
instruct2 = make_label('Step2: Do step 2', 0.2)

为什么不只是一个函数呢?你能告诉我你试过创建什么样的类吗?真的不清楚你想达到什么目的。明白了,谢谢。我想上一节课,因为我在这个周末学的,但这种方法很好用。谢谢。如果答案能解决你的问题,你能接受吗?否则,我也可以帮助您将代码转换为类。