0
我爲一個項目創建了兩個不同的窗口。第一個窗口是Demo1
使用Python的turtle模塊,第二個窗口是Demo2
使用Python的Tkinter。我爲每個窗口創建了兩個不同的程序,並且希望將兩個窗口組合成一個大型項目,並在一個類中包含最終項目。唯一的問題是,一旦我創建了窗口編碼的類,窗口2不起作用,並且這個類在一個更大的類中,並且窗口的所有編碼組合在一起。有沒有更好的方法來編寫這段代碼,或者有人可以修復它來正確運行。將Turtle和Tkinter模塊組合成一個大類
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()