Tkinter -Как получить метку, чтобы ответить на определенную функцию, которая была вызвана кнопкой - PullRequest
0 голосов
/ 08 февраля 2019

В настоящее время я использую Tkinter для графического интерфейса.Я работаю над программой LOGIC GATES, где я запрашиваю у пользователя два ввода (1 или 0).Я создал 5 функций (orF, andF, nandF, norF, xorF) и использовал метод GRID для позиционирования своих виджетов.

Я попытался получить метку, котораяЯ вызвал "al", чтобы представить результат после нажатия кнопки (OR, AND, NAND, NOR, XOR).

Виджет "al" не отвечает на функции и некоторые измои ярлыки были обрезаны.

Как мне создать программу, позволяющую пользователю нажимать на кнопку, чтобы результат (1 или 0) был представлен на другом ярлыке?

Вот код:

from tkinter import *

root = Tk()
root.title("Logic Gates Calculator")

Title = Label(root, text ="Using machine to calculate the correct LOGIC Gate 
from inputs",
              fg = "blue",
              bg = "#00ffff",
              font = "Calibri 22 bold italic")

Title.grid(row=0, column=0)

al = Label(root, text ="The result",
           fg = "white",
           bg = "blue",
           font = "Helvetica 22 bold")

al.grid(row=0, column=1)

first_il = Label(root, text ="Enter your first input: ",
                 fg = "white",
                 bg = "dark blue",
                 font = "Calibri 18 bold italic")

first_il.grid(row=2, column=0)

first_ie = Entry(root)
first_ie.grid(row=2, column=1)

second_il = Label(root, text ="Enter your second input: ",
                 fg = "white",
                 bg = "dark blue",
                 font = "Calibri 18 bold italic")

second_il.grid(row=3, column=0)

second_ie = Entry(root)
second_ie.grid(row=3, column=1)

def orF():
    input1 = first_ie.get()
    input2 = second_ie.get()

    if (input1 == "1" and input2 == "0") or (input2 == "1" and input1 == 
    "0"):
        al = Label(root, text ="1")

    if (input1 == "1" and input2 == "1"):
        al = Label(root, text ="1")

    if (input1 == "0" and input2 == "0"):
        al = Label(root, text ="0")

    else:
        al = Label(root, text ="Invalid input")

def andF():
    input1 = first_ie.get()
    input2 = second_ie.get()

    if (input1 == "1" and input2 == "0") or (input2 == "1" and input1 == "0"):
        al = Label(root, text ="0")

    if (input1 == "1" and input2 == "1"):
        al = Label(root, text ="1")

    if (input1 == "0" and input2 == "0"):
        al = Label(root, text ="0")

    else:
        al = Label(root, text ="Invalid input")

def norF():
    input1 = first_ie.get()
    input2 = second_ie.get()

    if (input1 == "1" and input2 == "0") or (input2 == "1" and input1 == "0"):
        al = Label(root, text ="0")

    if (input1 == "1" and input2 == "1"):
        al = Label(root, text ="0")

    if (input1 == "0" and input2 == "0"):
        al = Label(root, text ="1")

    else:
        al = Label(root, text ="Invalid input")

def nandF():
    input1 = first_ie.get()
    input2 = second_ie.get()

    if (input1 == "1" and input2 == "0") or (input2 == "1" and input1 == "0"):
        al = Label(root, text ="1")

    if (input1 == "1" and input2 == "1"):
        al = Label(root, text ="0")

    if (input1 == "0" and input2 == "0"):
        al = Label(root, text ="1")

    else:
        al = Label(root, text ="Invalid input")

def xorF(): 
    input1 = first_ie.get()
    input2 = second_ie.get()

    if (input1 == "1" and input2 == "0") or (input2 == "1" and input1 == "0"):
        al = Label(root, text ="1")

    if (input1 == "1" and input2 == "1"):
        al = Label(root, text ="0")

    if (input1 == "0" and input2 == "0"):
        al = Label(root, text ="0")

    else:
        al = Label(root, text ="Invalid input")



or_button = Button(root, text ="Click for OR",
                    fg = "blue",
                    bg = "yellow",
                    font = "Calibri 20 bold italic",
                    command = orF)

or_button.grid(row =1, column =1)

and_button = Button(root, text ="Click for AND",
                    fg = "blue",
                    bg = "red",
                    font = "Calibri 20 bold italic",
                    command = andF)

and_button.grid(row =1, column =2)


nor_button = Button(root, text ="Click for NOR",
                    fg = "blue",
                    bg = "green",
                    font = "Calibri 20 bold italic",
                    command = norF)

nor_button.grid(row =1, column =3)



nand_button = Button(root, text ="Click for NAND",
                    fg = "blue",
                    bg = "orange",
                    font = "Calibri 20 bold italic",
                    command = nandF)

nand_button.grid(row =1, column =4)


xor_button = Button(root, text ="Click for XOR",
                    fg = "blue",
                    bg = "Magenta",
                    font = "Calibri 20 bold italic",
                    command = xorF)


xor_button.grid(row =1, column =5)


root.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...