Home > Blockchain >  How do I have certain numbers that are being put into the program using tkinter affect another numbe
How do I have certain numbers that are being put into the program using tkinter affect another numbe

Time:04-22

I currently have a very simple tkinter app which prints the given text in the console with no issues. However I want a set of numbers (1, 2, 3, 4, 5, 6) to add 1 to another float (which in this case is the count, for anyone that counts cards in blackjack). I just can't seem to get it to work, though.

Here is my current code:


master = Tk()
e = Entry(master)
e.pack()
plusOne = [1, 2, 3, 4, 5, 6]
count = 0

e.focus_set()

def callback():
    print(e.get())

print("-------")

if e.get() in plusOne:
    count   1
    print(count)


b = Button(master, text = "OK", width = 10, command = callback)
b.pack()

mainloop()

However this just prints the number which was put into the text box. It would be useful to see which cards were played by just seeing user inputs and then the count printed underneath it.

Would anyone know how to help?

CodePudding user response:

You need to declare the input as an integer in this case since that is what you put into your list. Otherwise I believe the entry value will be viewed as a string. I modified the script to work, but I'm not sure the count 1 is going to do what you want for card counting purposes. You would need a running count to go or - to count face cards successfully. Hopefully this helps.

import tkinter as tk

plusOne = [1, 2, 3, 4, 5, 6] # these are integers 

def callback():
   value = int(entry_field.get()) # sets entry to match int from list set up
   entry_field.delete("0", tk.END)

   count = 0

   if value in plusOne:
      print(count   1)
      
root = tk.Tk()

entry_field = tk.Entry(root, width=20)
entry_field.pack()

button = tk.Button(root, text = "OK", width = 10, command= callback)
button.pack()



root.mainloop()

CodePudding user response:

Here is a much easier and straight forward way to achieve the result without making a GUI.

plusOne = [1, 2, 3, 4, 5, 6] # values for non face cards
minusOne = [10]# value for face cards
nothing = ""
count = 0

while True:
   try:
      value = int(input("Enter Value:")) 
      if value != nothing and value in plusOne: # make sure the entry isn't left blank
         count  = 1 # add to the count by value of 1
         print("The count is now:", count)
      elif value in minusOne:
         count -= 1 # reduce the count by a vlue of 1
         print("The count is now:", count)
   except:
      print("Value not an integer")# raises error for non integer

A quick idea for if you want to keep the GUI would maybe be like this:

import tkinter as tk
  
plus_count = [1, 2, 3, 4, 5, 6]
minus_count = [10]
var = [0] 
nothing = ""

def count_cards():
    value = int(entry_field.get())
    entry_field.delete("0", tk.END)

    if value != nothing and value in plus_count:
        for item in var:
            if item != nothing:
                add_count = item   1
                var.pop()
                var.append(add_count)
                print("The count is:", var)
    elif value != nothing and value in minus_count:
        for item in var:
            if item != nothing:
                subtract_count = item - 1
                var.pop()
                var.append(subtract_count)
                print("The count is:", var)
                
      
root = tk.Tk()

entry_field = tk.Entry(root, width=15, textvariable=tk.StringVar())
entry_field.pack()

button = tk.Button(root, text="Count", command= count_cards)
button.pack()

root.mainloop()
  • Related