How do i create a variable in tk.write()

Viewed 67

I am a newbie to python and stack overflow.How do we create a variable in the tkinter.write() function in python. the below object is my code.

import turtle as t
import itertools as it
import random
import tkinter as tk


def new_func():
    user_num = input("Type any number from 1-10 below")
    return user_num

user_num = new_func()
computer_num = random.randint(1,10)
t.fillcolor("red")
t.begin_fill()
t.color("red")
a= 4
for _ in it.repeat(None,a) :
    t.fd(100)
    t.lt(90)
t.end_fill()
t.write(user_num)
while user_num != computer_num :
    if user_num > computer_num :
        print("To high")
        user_num = input("type again : ")
    elif user_num < computer_num :
        print("To low")
        user_num = input("type again : ")
    else :
        print("Yay! you guessed it!")
2 Answers

You can use an f-string,

And there is no tkinter.write() function, You can use the Text or Label class in Tkinter

Example of text class:

some_variable = Text(tkinter_window, bg, fg, border, height, width, font, etc..

You can view all the parameters here: https://www.geeksforgeeks.org/python-tkinter-text-widget/

Then after that you can some_variable.insert(index, f"{variable_name} text")

The indexes are available in the Tkinter module, An example would be END

Example of label class:

some_variable = Label(window, text=f"{variable_name} text")

Importing tkinter isn't needed for this. Possibly, if you have advanced canvas requirements later on, but for now, it can all be done within the Turtle script. Your loop wouldn't loop that way. It needs to be wrapped within your new function, so that it can be called again, when required.

#! /usr/bin/env python3

import turtle as trt
import itertools as it
from time import sleep
import random

t, screen = trt .Turtle(),  trt .Screen()
screen .title( 'Take a guess' )

fontname = 'Arial'  ##  anytime you use a single equal sign, you are assigning a value to a variable.
fontsize = 33  ##  larger fontsize
fontstyle = 'normal'
f = ( fontname, fontsize, fontstyle )

computer_num = random .randint( 1, 10 )

##  wrap all your instructions in here, so that when you call new
def new_func():  ##  it can repeat from this point

    ##  turtle has a numinput box that ensures player types numbers
    ##  instead of letters, and lets you specify min / max values
    
    popup_title = 'Guess'
    message = 'Type any number from 1-10'
    default_value = 5

    ##  wrap it in an int, to get rid of decimal points
    user_num = int( screen .numinput( popup_title,  message,  default_value, minval = 1,  maxval = 10 ) )

    t .penup()
    t .clear()
    t .goto( -50, -25 )  ##  so when you jump turtle back home(), text is centered
    t .fillcolor( 'red' )
    t .begin_fill()
    t .color( 'red' )
    t .pendown()
    a = 4

    for _ in it .repeat( None, a ) :
        t .fd( 100 )
        t .lt( 90 )
    t .end_fill( )

    t .penup()
    t .home()
    t .color( 'black' )  ##  set to a diff' color than background, for readability

    t .write( user_num,  align = 'center',  font = f )
    t .penup()
    t .sety( -100 )
    t .pendown()

    if user_num > computer_num :
        t .write( 'Too high',  align = 'center',  font = f )
        sleep( 1 )
        new_func()  ##  call it, to start the loop again

    elif user_num < computer_num :
        t .write( 'Too low',  align = 'center',  font = f )
        sleep( 1 )
        new_func()

    else :
        t .write( 'Yay! you guessed it!',  align = 'center',  font = f )
        t .hideturtle()
        sleep( 3 )

new_func()  ##  call the function once, so it can begin.
Related