如何修复未提交的提交按钮(Tkinter)

时间:2019-07-18 19:38:26

标签: python tkinter

我正在制定一个虐待/欺凌意识计划(例如,告诉他人),您可以在其中选择问题和位置,然后输入名称并按提交。但是,我为Submit函数编写的代码没有注册任何输入。

我切换到“提交”按钮,因为以前的修订版难以为新用户使用。

from tkinter import *

global f
global l
f.set("")
l.set("")

def victim():
    b = "Victim"

def perp():
    b = "Perpetrator"

def victimhome():
            print("")
            print("Unsafe Situation Reported")
            print("Report of Victim")
            print("Location: Home")
            print("Name:", f.get(), l.get())
            print("")

        def victimschool():
            print("")
            print("Unsafe Situation Reported")
            print("Report of Victim")
            print("Location: School")
            print("Name:", f.get(), l.get())
            print("")

        def victimother():
            print("")
            print("Unsafe Situation Reported")
            print("Report of Victim")
            print("Location: Other")
            print("Name:", f.get(), l.get())
            print("")

        def perphome():
            print("")
            print("Unsafe Situation Reported")
            print("Report of Perpetrator")
            print("Location: Home")
            print("Name:", f.get(), l.get())
            print("")

        def perpschool():
            print("")
            print("Unsafe Situation Reported")
            print("Report of Perpetrator")
            print("Location: School")
            print("Name:", f.get(), l.get())
            print("")

        def perpother():
            print("")
            print("Unsafe Situation Reported")
            print("Report of Perpetrator")
            print("Location: Other")
            print("Name:", f.get(), l.get())
            print("")

def submit():
            if b == "Victim":
                x = 1
            elif b == "Perpetrator":
                x = 2
            else:
                x = 3

            if x == 1:
                if bb == 1:
                    victimhome()
                elif bb == 2:
                    victimschool()
                elif bb == 3:
                    victimother()
                else:
                    print("")
            elif x == 2:
                if bb == 1:
                    perphome()
                elif bb == 2:
                    perpschool()
                elif bb == 3:
                    perpother()
                else:
                    print("")
            elif x == 3:
                print("")
            else:
                print("")


victimbuttons1 = Radiobutton(self, text='Home', width = 30, variable=bb, value = 1, indicatoron = 0)

victimbuttons2 = Radiobutton(self, text='School', width = 30, variable=bb, value = 2, indicatoron = 0)

victimbuttons3 = Radiobutton(self, text='Other', width = 30, variable=bb, value = 3, indicatoron = 0)

victimentry1 = Entry(self, textvariable = f)
victimentry2 = Entry(self, textvariable = l)

victimsubmit = Button(self, text = 'Submit', command = submit)



perpbuttons1 = Radiobutton(self, text='Home', width = 30, variable=bb, value = 1, indicatoron = 0)

perpbuttons2 = Radiobutton(self, text='School', width = 30, variable=bb, value = 2, indicatoron = 0)

perpbuttons3 = Radiobutton(self, text='Other', width = 30, variable=bb, value = 3, indicatoron = 0)

perpentry1 = Entry(self, textvariable = f)
perpentry2 = Entry(self, textvariable = l)

perpsubmit = Button(self, text = 'Submit', command = submit)

root = Tk()
root.geometry('800x500')
f = StringVar()
l = StringVar()
victimbuttons1.grid(row = 4, column = 1, padx = 5, pady = 5)
victimbuttons2.grid(row = 5, column = 1, padx = 5, pady = 5)
victimbuttons3.grid(row = 6, column = 1, padx = 5, pady = 5)
victimentry1.grid(row = 9, column =1, padx = 5, pady = 5)
victimentry2.grid(row = 10, column =1, padx = 5, pady = 5)
victimsubmit.grid(row = 11, column = 1, padx = 5, pady = 10)
perpbuttons1.grid(row = 4, column = 2, padx = 5, pady = 5)
perpbuttons2.grid(row = 5, column = 2, padx = 5, pady = 5)
perpbuttons3.grid(row = 6, column = 2, padx = 5, pady = 5)
perpentry1.grid(row = 9, column = 2, padx = 5,  pady = 5)
perpentry2.grid(row = 9, column = 2, padx = 5,  pady = 5)
perpsubmit.grid(row = 11, column = 2, padx = 5, pady = 10)
root.title('Bullying and Abuse Hotline')
root.mainloop()

我正在尝试让程序在按下按钮时将一些信息打印到终端中,但此刻它只是创建换行符。

1 个答案:

答案 0 :(得分:0)

您永远不会调用会设置变量b的函数受害者()或perp(),因此x值始终为3并执行打印。

还可以将global b放在受害者和perp两个函数中,否则b将是局部的。

一些建议对字符串“受害者”和“犯罪者”使用常量:

VICTIM = "Victim"
PERPETRATOR = "Perpetrator"

对变量的命名也要比x或b(也许是person_kind)更好。 您还可以删除if b == ...然后设置x,然后只使用person_kind(或您选择的任何好听的名字)

if person_kind == VICTIM:
    ... do something for Victim
elif person_kind == PERPETRATOR:
    ... do something for Perpetrator

以此类推

如果可能,还尝试避免使用全局变量。 祝你好运