-1

I was trying to create an app on tkinter but i have some issues. so the problem is with the button start, it must show the result on the screen when pressed, but i dont know what should i write in the command line to make it work. i was trying something like command = game(answ) but this doesnt work because when you start the programm it already shows the result so the start button has no point. what can i do? lower is the code where game has no argument so it doesn t work

import tkinter as tk
from tkinter import filedialog, Text
import os
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import simpledialog
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo


count_w = 0
count_l = 0
a = []
ls = []
ws = []
version = '0.1 PreAlpha'



def asking():
    global win, win2, answ, bbok
    answ = simpledialog.askinteger("input string", " Choose 1 / 2")
    if answ == 1 or answ == 2:
        win2 = tk.Toplevel()
        win2.wm_title("Great Choice")
        bbok = tk.Button(win2, text=f'Selected {answ}', command=goback2)
        bbok.pack()
        root2.destroy()
    else:
        win = tk.Toplevel()
        win.wm_title("Error")
        bok = tk.Button(win, text="Try Again! Insert other value", command=goback)
        bok.pack()
def goback():
    choiceframe.pack()
    win.destroy()
def goback2():
    win2.destroy()
    bbok.destroy()
def goback3():
    root.destroy()
def kekes():
    win = tk.Toplevel(root, command=showinfo())
    win.wm_title("Error")
def game(answ):
    adv = True
    global a, ws, ls, count_w, count_l
    bullet = random.randint(1, 6)
    if answ == 1:
        enemy = 2
    elif answ == 2:
        enemy = 1
    choice = answ
    answ, en1 = 0, 0
    i = 0
    cnt = 0
    while adv == True:
        i = i + 1
        cnt = i * 2
        if choice == 1:
            answ = en1 + 1
            en1 = answ + 1
        elif choice == 2:
            en1 = answ + 1
            answ = en1 + 1
        if answ == bullet:
            print('You lost after ', cnt, " shot")
            losing = 'You lost king '
            ws.append(losing)
            adv = False
            count_l += 1
        elif en1 == bullet:
            print("Enemy lost after ", cnt, " shot")
            wining = 'You won king '
            ls.append(wining)
            adv = False
            count_w +=1
    print(f'Me {count_w} :  {count_l} Enemy')
    a.append(cnt)

wl = [ls, ws]

def winer_looser(wl):
    global restart, wining_an
    for lists in wl:
        for text in lists:
            for words in text.split():
                if words == "won":
                    wining_an = tk.Toplevel()
                    wining_an.wm_title("You won")
                    close_butt2 = tk.Button(wining_an, text=" Close ", command = goback3)
                    close_butt2.pack()
                    restart = tk.Button(wining_an, text = "Restart", command = restartgame)
                    restart.pack(expand = 1)
                    print("You")
                if words == "lost":
                    losing_an = tk.Toplevel()
                    losing_an.wm_title("You lost")
                    close_butt = tk.Button(losing_an, text="Close", command=goback3)
                    close_butt.pack()
                    restart = tk.Button(wining_an, text="Restart", command=restartgame)
                    restart.pack(expand=1)
                    print("Enemy")

def restartgame():
    game(answ)


root2 = tk.Tk()

root2.title(f'choser {version}')
choiceframe = tk.Frame(root2, bg='white')
choiceframe.place(relwidth=0.15, relheight=0.025, relx=0.675, rely=0.75)
choiceframe.pack()
buttonframe = tk.Frame(choiceframe, bg='white')
buttonframe.place(relwidth=0.15, relheight=0.025, relx=0.675, rely=0.675)
buttonframe.pack()
B1 = tk.Button(buttonframe, bg='grey', text='Make your choice ', command=asking)
B1.pack(expand=1)

root2.mainloop()



root = tk.Tk()
root.title(f'Russian Roullete {version}')

canvas = tk.Canvas(root, height=800, width=700, bg="#FF5733")
canvas.pack()

mainframe = tk.Frame(root, bg="grey")
mainframe.place(relwidth=0.3, relheight=0.1, relx=0.6, rely=0.8)

start = tk.Button(mainframe, text=" Start !", padx=60, pady=20, fg="black", bg='grey', command= game(answ))
start.pack(expand=1)

frame_count = tk.Frame(root, bg="grey")
frame_count.place(relwidth=0.16, relheight=0.05, relx=0.675, rely=0.72)




for x in a:
    moves = tk.Label(frame_count, text=f'Number of moves {x}', bg='grey')
    moves.pack(expand=1)

root.mainloop()

print(f'Me {count_w} :  {count_l} Enemy')```
AppleCidar
  • 41
  • 5

1 Answers1

0

when you set

command = game(answ)

it's calling game immediately and passing the return value (wich will be None), not the function itself. if you have to pass an arg, use a lambda function like so:

command = lambda:game(answ)
Hadrian
  • 917
  • 5
  • 10
  • Thanks you helped me. Now i have another issue :) i created a label in the function, and now when i pack it ( my programm has an option to run an unlimited amount of time (it can be restarted)) it shows the result of the previous run, can i fix it somehow? – AppleCidar Oct 10 '20 at 15:34