首页 > 解决方案 > 如何修复提交按钮未提交(Tkinter)

问题描述

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

我切换到提交按钮是因为以前的版本很难用于新用户。

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

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

标签: pythontkinter

解决方案


您永远不会调用将设置变量 b 的函数victim() 或 perp(),因此 x 值始终为 3,然后您进行打印。

还要把global b受害者和 perp 这两个函数放在里面,否则 b 将是本地的。

一些建议使用字符串“Victim”和“Perpetrator”的常量:

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

等等

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


推荐阅读