ömer sarı ömer sarı - 3 months ago 12
Python Question

Radiobutton Selection

I am trying to make a game by combining Turtle and Tkinter. My code is working good but I have a problem with selection of

Radiobutton
part. When I try to select the "Black" (colored) radiobutton, I am not able to select it, however, there is no problem with other colors selection. Another problem is that I would like to use "No color" option and when the user selects it, shape must be drawn with no filling. But it does not work, it gets filled with a color selected automatically. I do not get how it is selected automatically. How can I fix that problem?

import tkinter
from tkinter import *
import turtle
import sys
sc=Tk()
sc.geometry("1000x1000+100+100")

var= IntVar()
pensize=StringVar()
angle=StringVar()
radius=StringVar()
lenght=StringVar()

#FRAMES
fr1=Frame(sc,height=200,width=200,bd=4,bg="light green",takefocus="",relief=SUNKEN)
fr1.grid(row=0,column=0,sticky=(N,E,W,S))
#FR2
fr2=Frame(sc,height=200,width=200,bd=4,bg="light green",takefocus="",relief=SUNKEN)
fr2.grid(row=1,column=0,sticky=(N,E,W,S))
#FR3
fr3=Frame(sc,height=200,width=200,bd=4,bg="light green",takefocus="",relief=SUNKEN)
fr3.grid(row=2,column=0,sticky=(N,E,W,S))
#FR4
fr4=Frame(sc,height=500,width=600,bd=4,bg="light green",takefocus="",relief=SUNKEN)
fr4.grid(row=2,column=2,sticky=(N,E,W,S))
#FR5
fr5=Frame(sc,height=100,width=600,bd=4,bg="gray",takefocus="",relief=SUNKEN)
fr5.grid(row=1,column=2,sticky=(N,E,W,S))

#Canvas
canvas = Canvas(fr4,width=750, height=750)
canvas.pack()

#Turtle
turtle1=turtle.RawTurtle(canvas)
turtle1.color("blue")
turtle1.shape("turtle")

#Functions
def rectangle():
turtle1.begin_fill()
colorchoices()
turtle1.pensize(int(pensize.get()))
for i in range(4):
turtle1.forward(int(lenght.get()))
turtle1.left(90)
turtle1.end_fill()

def circle():
turtle1.begin_fill()
turtle1.pensize(int(pensize.get()))
colorchoices()
turtle1.circle(int(radius.get()))
turtle1.end_fill()

def triangle():
turtle1.begin_fill()
colorchoices()
turtle1.pensize(int(pensize.get()))
turtle1.forward(int(lenght.get()))
turtle1.left(120)
turtle1.forward(int(lenght.get()))
turtle1.left(120)
turtle1.forward(int(lenght.get()))
turtle1.end_fill()

def line100 ():
turtle1.penup()
turtle1.pensize(int(pensize.get()))
turtle1.forward(int(lenght.get()))
turtle1.pendown()

def linel90 ():
turtle1.penup()
turtle1.left(int(angle.get()))
turtle1.pendown()

def liner90 ():
turtle1.penup()
turtle1.pensize(int(pensize.get()))
turtle1.right(int(angle.get()))
turtle1.pendown()

def linebc100 ():
turtle1.penup()
turtle1.begin_fill()
colorchoices()
turtle1.pensize(int(pensize.get()))
turtle1.backward(int(lenght.get()))

turtle1.end_fill()
turtle1.pendown()

def line():

turtle1.begin_fill()
colorchoices()
turtle1.pensize(int(pensize.get()))
turtle1.forward(int(lenght.get()))
turtle1.end_fill()

def colorchoices():
selection=var.get()
for i in range(7):
if selection==1:
turtle1.fillcolor("red")
elif selection==2:
turtle1.fillcolor("blue")
elif selection==3:
turtle1.fillcolor("purple")
elif selection==4:
turtle1.fillcolor("yellow")
elif selection==5:
turtle1.fillcolor("black")
elif selection==6:
turtle1.fillcolor("green")
else:
return

def clear():
turtle1.clear()




but1=Button(fr1,text="Clear",command=clear).pack(side=LEFT)
# shapes
shpc=PhotoImage(file="D://python//ımage/circle.png")
shptr=PhotoImage(file="D://python//ımage/rectangular.png")
shpsq=PhotoImage(file="D://python//ımage/square.png")
shpl=PhotoImage(file="D://python//ımage/line.png")
shphep=PhotoImage(file="D://python//ımage/shapes.png")

#Entry and labels
PenSize=Button(fr5,text="PenSize",font=("Helvetica",14),fg="black").grid(row=0,column=0,columnspan=1,sticky=W)
Angle=Button(fr5,text="Angle",font=("Helvetica",14),fg="black").grid(row=1,column=0,columnspan=1,sticky=W)
Radius=Button(fr5,text="Radius",font=("Helvetica",14),fg="black").grid(row=2,column=0,columnspan=1,sticky=W)
Lenght=Button(fr5,text="Lenght",font=("Helvetica",14),fg="black").grid(row=3,column=0,columnspan=1,sticky=W)
pensize=Entry(fr5)
angle=Entry(fr5)
radius=Entry(fr5)
lenght=Entry(fr5)
pensize.grid(row=0,column=1,columnspan=2,sticky=W)
angle.grid(row=1,column=1,columnspan=2,sticky=W)
radius.grid(row=2,column=1,columnspan=2,sticky=W)
lenght.grid(row=3,column=1,columnspan=2,sticky=W)



#optionmenu
ColorOption=Button(fr5,text="ColorOptions",font=("Helvetica",14),fg="black").grid(row=4,column=0,sticky=W)
R1 = Radiobutton(fr5, text="RED", variable=var, value=1,bg="red").grid(row=4,column=1,sticky=W)
R2 = Radiobutton(fr5, text="BLUE", variable=var, value=2,bg="blue").grid( row=4,column=2,sticky=W)
R3 = Radiobutton(fr5, text="PURPLE", variable=var, value=3,bg="purple").grid( row=4,column=3,sticky=W )
R4 = Radiobutton(fr5, text="YELLOW", variable=var, value=4,bg="yellow").grid( row=4,column=4,sticky=W)
R5 = Radiobutton(fr5, text="BLACK", variable=var, value=5,bg="black",fg="white").grid( row=4,column=5,sticky=W )
R6 = Radiobutton(fr5, text="GREEN", variable=var, value=6,bg="green").grid( row=4,column=6,sticky=W)
R7 = Radiobutton(fr5, text="NO COLOR", variable=var, value=7).grid( row=4,column=7,sticky=W)

#Buttons
but1=Button(fr2,text="Forward",command=line100).grid(row=1,column=2,sticky=(N,E,W,S))
but1=Button(fr2,text="Backward",command=linebc100 ).grid(row=1,column=0,sticky=(N,E,W,S))
but1=Button(fr2,text="Turn90",command=linel90).grid(row=0,column=1,sticky=(N,E,W,S))
but1=Button(fr2,text="Turn(-90)",command=liner90).grid(row=2,column=1,sticky=(N,E,W,S))
#shp1=Label(fr5,text="SHAPES",image=shphep).pack(fill=X)
shp2=Button(fr3,text="Circle",height=200,width=200,image=shpc,command=circle).pack()
shp3=Button(fr3,text="Triangular",height=200,width=200,image=shptr,command=triangle).pack()
shp4=Button(fr3,text="Square",height=200,width=200,image=shpsq,command=rectangle).pack()
shp5=Button(fr3,text="Line",height=200,width=200,compound=LEFT,image=shpl,command=line).pack()


sc.deiconify()
sc.mainloop()

Answer

For your second problem:

from tkinter import *
root = Tk()

selvar = StringVar()
selvar.set(' ')

rb1 = Radiobutton(root, variable=selvar, value='a')
rb1.pack()
rb2 = Radiobutton(root, variable=selvar, value='b')
rb2.pack()

This is for StringVar. And for IntVar:

from tkinter import *
root = Tk()

selvar = IntVar()
selvar.set(0)

rb1 = Radiobutton(root, variable=selvar, value=1)
rb1.pack()
rb2 = Radiobutton(root, variable=selvar, value=2)
rb2.pack()

UPDATE: I figured out that it works fine if I do not use var.set(0) for IntVar() (if I am starting from 1 not 0. Actually, 0 is what makes it selected at the program start). But if it does not work for you without setting it up to 0, then do it. It is actually making it safe anyway.

And for your first problem: You have selected black for background, and white for foreground color. Now when you click on this radiobutton, the dot does not get shown because the color of the dot is white according to the foreground color. Foreground color is not only applied to the text color of a radiobutton, but also to the color of the dot of a radiobutton. So white+white cannot be shown.

Comments