Python Forum
[Tkinter] How to compare two variables correctly in tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to compare two variables correctly in tkinter
#1
I have a program for a motel. Basically the user enters a room number and then the program is supposed to check to see if the room number had already been entered. But when I purposely enter the same room number, it doesn't catch the error but it should. Line 67 is where it compares.


from tkinter import *
import tkinter as tk
from tkinter import ttk
import sqlite3

def enterroomnumber():
    enterroom = Toplevel()
    enterroom.title('Enter Room Inventory')
    enterroom.geometry("800x800+500+100")

    global entry1
    global my_box
    global mycombo

    label1=Label(enterroom, text="Room#")
    label1.place(x=10, y=10)
    entry1 = Entry(enterroom, width=10)
    entry1.place(x=70, y=10)

    options = [
        "Select Room Type",
        "NQ1 - Non Smoking Queen",
        "NQQ1 - Non Smoking Double Queen",
        "Q11 - Smoking Queen",
        "QQ1 - Smoking Double Queen",
        "NK1 - Non Smoking king",
        "K11 - Smoking King",
        "PNQ1 - Non Smoking Handi-Cap",
        "SK1 - Smoking Suit King",
        "SNK1 - Non Smoking Suite King",
    ]

    mycombo = ttk.Combobox(enterroom, width=40, value=options)
    mycombo.place(x=10, y=40)
    mycombo.current(0)

    my_box = Listbox(enterroom, height=30, width=30)
    my_box.place(x=500, y=20)
    boxlabel=Label(enterroom, text="Rooms Already Entered")
    boxlabel.place(x=500, y=0)

    conn = sqlite3.connect('roominventory.db')
    c = conn.cursor()
    c.execute("SELECT * FROM rooms")
    records = c.fetchall()

    for record in records:
        my_box.insert(END, record[0:1])

    conn.commit()
    conn.close()

    entry1.focus()

    btnenterroom = Button(enterroom, text="Enter Room", width=10, command=setroom)
    btnenterroom.place(x=10, y=70)


def setroom():
    conn = sqlite3.connect('roominventory.db')
    c = conn.cursor()
    c.execute("SELECT * FROM rooms")
    records = c.fetchall()

    for record in records:

        if entry1.get()==record[0]:
            messagebox.showwarning("Warning!", "You already entered this room#.", parent=enterroom)


    jamin = mycombo.get()[0:4]
    jamin = jamin.strip()

    e2 = ""
    for x in range(0, 50):
        e2 = my_box.get(x)

        if e2[0:2] == "":
            break


        

    conn = sqlite3.connect('roominventory.db')
    c = conn.cursor()
    c.execute("INSERT INTO rooms VALUES (:number, :type, :rate, :vacant, :clean, :ooo, :r1, :r2, :r3, :r4, :r5)",

              {
                  'number': entry1.get(),
                  'type': jamin,
                  'rate': 0,
                  'vacant': "Y",
                  'clean': "Y",
                  'ooo': "N",
                  'r1': "N",
                  'r2': "N",
                  'r3': "N",
                  'r4': "N",
                  'r5': 0,
              }

              )

    conn.commit()
    conn.close()

    my_box.delete(0, END)

    conn = sqlite3.connect('roominventory.db')
    c = conn.cursor()
    c.execute("SELECT * FROM rooms")
    records = c.fetchall()

    for record in records:
        my_box.insert(END, record[0:2])

    conn.commit()
    conn.close()

    mycombo.set("Select Room Type")

    entry1.delete(0, END)
    entry1.focus()


root = Tk()
root.geometry("400x400")

btn1=Button(root, text="Click to enter room", command=enterroomnumber)
btn1.pack()



root.mainloop()
Reply
#2
if entry1.get()==record[0]:
entry1.get() returns a string if record[0] is a number, the string will converting to do the comparision.
Reply
#3
That worked. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 793 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  [Tkinter] Tkinter bringing variables from 1 frame to another zukochew 6 7,065 Dec-26-2019, 05:27 AM
Last Post: skm
  [Tkinter] String variables in Tkinter rturus 2 1,175 Jun-12-2019, 11:21 AM
Last Post: rturus
  Int Variables in different Tkinter windows only returning 0 harry76 3 2,393 May-26-2019, 10:24 AM
Last Post: Yoriz
  Multi windows in tkinter buttons not responding correctly curtjohn86 13 7,372 Jul-01-2017, 03:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020