当我将存档导入其他代码时,Tkinter 条目为 0

Tkinter Entry is 0 when i import the archive to other code

好吧,我是初学者。 我用 tkinter 创建了一个程序,为了组织起来,我将它们分成 3 个辅助档案和 1 个主体,它们都是 .py,如果它们单独执行,它们就可以工作,但是当我将 secundaries 导入主体代码时,条目就变成了在 0

看:

from tkinter import *

def average_opt():
    import average


def necessarygrade_opt():
    import necessary_grade


def yourgrade_opt():
    import grade


root = Tk()
root.title('Average Calculator')
root.geometry('200x200')

# options for user
Button(root, text='Average', command=average_opt, width=20).place(x=25, y=30)
Button(root, text='Necessary Grade', command=necessarygrade_opt, width=20).place(x=25, y=90)
Button(root, text='Your Grade', command=yourgrade, width=20).place(x=25, y=150)

root.mainloop()

平均值是:

from tkinter import *

root2 = Tk()
root2.title('Average Calculator')
root2.geometry('400x400')


def calculate():
    average = (2 * grade1.get() + 2 * grade2.get() + grade3.get()) / 5
    Label(root2, text='Your average is {}'.format(average), font=('comic sans ms', 16, 'bold')).place(x=55, y=250)


# Labels: Show informations for user
grade1_label = Label(root2, text='Test 1 grade:', font=('comic sans ms', 13, 'bold')).place(x=35, y=30)
grade2_label = Label(root2, text='Test 2 grade:', font=('comic sans ms', 13, 'bold')).place(x=25, y=80)
grade3_label = Label(root2, text='Work's Grade:', font=('comic sans ms', 13, 'bold')).place(x=15, y=130)

# Entrys: Colect the informations from users
grade1 = IntVar()
grade2 = IntVar()
grade3 = IntVar()

grade1_entry = Entry(root2, textvariable=grade1, width=30).place(x=180, y=37)
grade2_entry = Entry(root2, textvariable=grade2, width=30).place(x=180, y=87)
grade3_entry = Entry(root2, textvariable=grade3, width=30).place(x=180, y=137)

# Buttons: Execute a command
Button(root2, text='Calculate', command=calculate, width=50).place(x=20, y=180)

root2.mainloop()

所需成绩为:

from tkinter import *

root3 = Tk()
root3.title('Nota Necessária')
root3.geometry('400x400')


def calculate2():
    nota = (corrects.get() / total.get()) * 10
    Label(root3, text='Your grade was {}'.format(grade), font=('comic sans ms', 14, 'bold')).place(x=70, y=250)


# Labels: Show informations for user
escores_totais_label = Label(root3, text='Total:', font=('comic sans ms', 13, 'bold')).place(x=25, y=30)
escores_atingidos_label = Label(root3, text='Corrects:', font=('comic sans ms', 13, 'bold')).place(x=15, y=80)


# Entry: Colect the informations from users
total = IntVar()
corrects = IntVar()

escores_totais_entry = Entry(root3, textvariable=total, width=30).place(x=180, y=37)
escores_atingidos_entry = Entry(root3, textvariable=corrects, width=30).place(x=180, y=87)


# Buttons: Execute a command
Button(root3, text='Calculate', command=calculate2, width=50).place(x=20, y=180)

root3.mainloop()

成绩是:

from tkinter import *

root4 = Tk()
root4.title('Nota')
root4.geometry('400x400')


def calculate3():
    bimestral = (5 * target.get() - 2 * grade1.get() - grade2.get()) / 2
    Label(root4, text='The grade to arrive\in the average {} is {}'.format(target.get(), bimestral),
          font=('comic sans ms', 14, 'bold')).place(x=55, y=250)


# Labels: Show informations for user
grade1_label = Label(root4, text='Test 1:', font=('comic sans ms', 13, 'bold')).place(x=35, y=30)
grade2_label = Label(root4, text='Work's Grade:', font=('comic sans ms', 13, 'bold')).place(x=15, y=80)
target_label = Label(root4, text='Target Average:', font=('comic sans ms', 13, 'bold')).place(x=20, y=130)

# Entry: Colect the informations from users
grade1 = IntVar()
grade2 = IntVar()
target = IntVar()

grade1_entry = Entry(root4, textvariable=grade1, width=30).place(x=180, y=37)
grade2_entry = Entry(root4, textvariable=grade2, width=30).place(x=180, y=87)
target_entry = Entry(root4, textvariable=target, width=30).place(x=180, y=137)

# Buttons: Execute a command
Button(root4, text='Calculate', command=calculate3, width=50).place(x=20, y=180)

root4.mainloop()

我知道这很乱,因为我在编码中迷失了自己。 这是基于我学校的系统,我不知道其他地方是否使用它。

提前致谢。 (对不起我的英语哈哈)

您正在所有文件中创建 Tk() 的实例。您只能为整个程序创建一个 1Tk() 实例。


1 从技术上讲,你可以做不止一个,但你需要深入了解当你这样做时会发生什么。作为初学者,您应该将只能创建一个作为硬性规定。

基本上你可以将root? = Tk()更改为root? = Toplevel()并删除三个模块.py文件中的root?.mainloop()

您还需要解决以下问题:

在主体中,对于 Your Grade 按钮,command=yourgrade 应该是 command=yourgrade_opt

grade.py中,对grade2_label的文本使用"text="Work's Grade:"

average.py中,对grade3_label的文本使用"text="Work's Grade:"

calculate2()里面necessary_grade.py.format(grade)应该是.format(nota)