我为一个项目创建了两个不同的窗口。第一个窗口是Demo1
,使用Python的turtle模块,第二个窗口是Demo2
,使用Tkinter for Python。我为每个窗口创建了两个不同的程序,并希望将两个窗口组合成一个完整的大项目窗口中的最终项目。唯一的问题是,一旦我创建了一个用于窗口编码的类,窗口2就无法工作,而且这个类位于一个更大的类中,窗口的所有编码都被组合在一起。是否有更好的方法来编写此代码,或者有人可以修复它以便正确运行。
from tkinter import *
import tkinter as tk
import threading
import random
from graphics import *
import turtle
from time import sleep
class Intro():
def __init__(self,master):
self.master = master
self.master.geometry("800x300")
self.master.title("Welcome")
self.master.configure(background = 'blue')
self.label1 = Label(self.master,text ="Event Driven Programming Demonstration", fg = "white", bg = "blue",font = ("Purisa",18)).grid(row =1,column = 2)
self.label2 = Label(self.master,text ="Program created by Ariel Lyons & Aaron Brown for CSC322 FSU 2014",font = ("Purisa",18), fg = "white",bg = "blue").grid(row = 2, column = 2)
self.quitButton = Button(self.master,text ="Quit", fg="Black",bg = "red",command=self.finish).grid(row =18,column =2)
self.nextButton = Button(self.master,text = "Demo 1", fg ="Black",bg = "yellow",command =self.goToDemo1).grid(row = 14, column =2)
self.Demo2Button = Button(self.master,text = "Demo 2", fg = "Black", bg = "yellow",command = self.goToDemo2).grid(row = 16, column = 2)
self.master.bind("<Return>", self.goToDemo1)
def goToDemo1(self):
threading.Timer(5.0, goToDemo1).start()
root2 = Toplevel(self.master)
myGUI = Demo1(root2)
def goToDemo2(self):
try:
app = Demo2()
app.th.start()
app.win.mainloop()
app.th.join()
finally:
app.th.close()
app.close()
def goToDemo1(self):
root2 = Toplevel(self.master)
myGUI = Demo1(root2)
def finish(self):
self.master.destroy()
class Demo1():
def __init__(self,t):
self.t = turtle.Turtle()
ts = self.t.getscreen()
ts.register_shape("plane.gif")
self.t.shape("plane.gif")
self.t.pencolor("white")
ts.onscreenclick(self.t.goto)
ts.bgpic('sky.gif')
self.t.circle(50,400)
self.t.circle(100,500)
def goToDemo2(self):
root3=Toplevel(self.t)
myGUI = Demo2(root3)
def finish(self):
self.t.destroy()
class Demo2():
def __init__(self):
self.win = GraphWin('Demo2', 800, 600)
self.win.setBackground("light green")
self.th = threading.Thread(target=self.FlashThread, daemon=False)
def FlashThread(self):
while not self.win.isClosed():
count = random.randint(0, 8)
t = threading.Timer(1.0, self.flash, [count])
t.start()
t.join()
def flash(self, count):
try:
diameter = 50
centers = ((55,55), (170,55), (285,55), (55,170), (170,170),
(285,170), (55,285), (170,285), (285,285))
circles = list()
for point in centers:
c = Circle(Point(point[0], point[1]), diameter)
circles.append(c)
c.setFill("blue")
c.draw(self.win)
circles[count].setFill("yellow")
mouseClick = self.win.getMouse()
correctMessage = Text(Point(self.win.getWidth()/2, 20), 'Correct!')
incorrectMessage = Text(Point(self.win.getWidth()/2, 20), 'Incorrect,Try Again')
leftX = centers[count][0] - diameter
rightX = centers[count][0] + diameter
upperY = centers[count][1] - diameter
lowerY = centers[count][1] + diameter
if (upperY < mouseClick.y < lowerY) and (leftX < mouseClick.x < rightX):
correctMessage.draw(self.win)
print("Correct!")
else:
incorrectMessage.draw(self.win)
print("Incorrect, Try Again")
except:
self.win.close()
def main():
root = Tk()
myGUIIntro = Intro(root)
root.mainloop()
if __name__ =="__main__":
main()
答案 0 :(得分:-1)
我想我不理解你的问题。您是否尝试创建两个窗口,但只有一个类?你的意思是窗口2不起作用是什么意思?