我想通过结合Turtle和Tkinter来制作游戏。我的代码运行良好,但我在选择Radiobutton
部分时遇到问题。当我尝试选择“黑色”(彩色)单选按钮时,我无法选择它,但是,其他颜色选择没有问题。另一个问题是我想使用“无颜色”选项,当用户选择它时,必须绘制没有填充的形状。但它不起作用,它会自动填充颜色。我不明白它是如何自动选择的。我该如何解决这个问题?
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()
答案 0 :(得分:2)
对于你的第二个问题:
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()
这是StringVar
。对于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()
更新:我发现如果我不对var.set(0)
使用IntVar()
,它会正常工作(如果我从1开始不是0.实际上,0是什么在程序开始时选择它。但是如果它没有设置为0就不适合你,那就去做吧。无论如何它实际上是安全的。
对于您的第一个问题:您选择了黑色作为背景,白色选择了前景色。现在,当您单击此单选按钮时,点不会显示,因为根据前景色,点的颜色为白色。前景色不仅适用于单选按钮的文本颜色,还适用于单选按钮的点的颜色。因此无法显示白色+白色。
答案 1 :(得分:0)
这是我更正后的代码
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(8):
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("orange")
elif selection==6:
turtle1.fillcolor("green")
elif selection==7:
turtle1.fillcolor("")
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="ORANGE", variable=var, value=5,bg="orange").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()