Boinbo
Boinbo

Reputation: 9

How to assign button in TkInter to my script?

I'm new to python, and this site, and I have a question.


#App design below
import tkinter as tk

Height = 700
Width = 800

def test_function(entry):
    print("This is the entry:", entry)

root = tk.Tk()

canvas = tk.Canvas(root, height=Height, width=Width)
canvas.pack()

frame = tk.Frame(root, bg='black')
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')

entry = tk.Entry(frame, font=40)
entry.place(relwidth=0.65, relheight=1)

button = tk.Button(frame, text="Random Click!", font=40, command=lambda: test_function(entry.get()))
button.place(relx=0.7, relheight=1, relwidth=0.3)

lower_frame = tk.Frame(root, bg='#80c1ff', bd=10)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')

label = tk.Label(lower_frame)
label.place(relwidth=1, relheight=1)

root.mainloop
#Work below
import random
min = 1
max = 6

roll_again = "yes"

while roll_again == "yes" or roll_again == "y" or roll_again == " y" or roll_again == "Yes" or roll_again == " yes" or roll_again == " Y":
    print ("Rolling the dices...")
    print ("The values are....")
    val_a = (random.randint(min, max))
    val_b = (random.randint(min, max))
    print(val_a)
    print(val_b)
    total = val_a + val_b
    print("The total amount added up is " + str(total))
    roll_again = input("Roll the dices again?")

while roll_again == "no" or roll_again == "n" or roll_again == " n" or roll_again == "No" or roll_again == " no" or roll_again == " N":
    coin_flip = input("Do you want to flip a coin then?")
    while coin_flip == "yes" or coin_flip == "Yes" or coin_flip == " yes" or coin_flip == " Yes" or coin_flip == "y" or coin_flip == "Y" or coin_flip == " y" or coin_flip == " Y":
        print("Flipping coin...")
        print("You got...")
        coinfliplist = ["Heads", "Tails"]
        print(random.choice(coinfliplist))
        coin_flip = input("Do you want to flip again?")
    while coin_flip == "no" or coin_flip == "n":
            exit()

I was watching a little video on how to make an app on python, and I followed it to a point, but now I need help because I don't get the video. I want to assign the button to execute everything below the #work below comment, and the entry to be used for typing in Y or N. Can someone help me as to how I would go about this? Thanks!

Upvotes: 0

Views: 93

Answers (2)

Lidor shimoni
Lidor shimoni

Reputation: 93

if I'm getting you intentions correctly then this should help:

import Tkinter

root = Tkinter.Tk()

roll_again = ""

def workCallBack(choice):
   roll_again = choice

entry = tk.Entry(frame, font=40)
entry.place(relwidth=0.65, relheight=1)

button = tk.Button(frame, text="Random Click!", font=40, command= lambda: workCallBack(entry.get()))
button.place(relx=0.7, relheight=1, relwidth=0.3)

root.mainloop() # should be at the bottom

since you are new to python it's fine, but I strongly recommend tidy up you code with some functions to reduce duplicated code, etc..

Upvotes: 0

Warisul
Warisul

Reputation: 116

First, you need to bring you 'root.mainloop()' to the bottom of the code. Note that you forgot to add the parentheses after root.mainloop(). Check your code please.

If you want your button to execute all that is below your comment, then you have to define that particular snippet within a function and then set the command of the button to that function. But you have to define the function before defining your button. It should look something like this:

def arbitrary_function_name():
    #Put all the code below your comment in here. Don't forget to indent.

button = tk.Button(frame, text="Random Click!", font=40, command=arbitrary_function_name) #Remember, don't put the parentheses after the function name in the command.
button.place(relx=0.7, relheight=1, relwidth=0.3)

Try cleaning out your code a bit too. I tried running it and it crashed. If it runs for you then that's good.

Upvotes: 1

Related Questions