from Tkinter import *
import Tkinter as ttk
from ttk import *
master = Tk()
master.title("Learn Spanish")
canvas = Canvas(master, width = 600, height = 600, bg = 'orange')
canvas.grid(row=0, column=0, rowspan=2, columnspan=2)
title = canvas.create_text(300, 100, text = 'Learn To Speak Spanish', font = ('Freestyle Script', 60), fill = 'firebrick')
text = canvas.create_text(300, 250, text = 'Welcome! What are you ready \n to learn Spanish today?', font = ('Freestyle Script', 35), fill = 'firebrick')
def nextScreen():
canvas.delete("all")
canvas.create_text(300, 125, text = 'Select one of the activities below to get started.', font = ('Freestyle Script', 30), fill = 'firebrick')
btn4 = ttk.Button(master, bg='white', command=wordStudy, text='Word Study', font=('Freestyle Script', 35))
canvas.create_window(300, 300, width=300, height=90, window=btn4)
btn5 = ttk.Button(master, bg='white', command=matchGame, text='Matching Game', font=('Freestyle Script', 35))
canvas.create_window(300, 400, width=300, height=90, window=btn5)
btn6 = ttk.Button(master, bg='white', command=quiz, text='Quiz Yourself', font=('Freestyle Script', 35))
canvas.create_window(300, 500, width=300, height=90, window=btn6)
def quiz():
canvas.delete("all") # haven't worked on this one yet
def matchGame():
canvas.delete("all") # haven't worked on this one yet either
def wordStudy(): #i'm right here
canvas.delete("all")
Canvas(master, width = 600, height = 600, bg = 'orange')
canvas.create_text(300, 50, text = 'Study the terms below.', font = ('Freestyle Script', 30), fill = 'firebrick')
btn = ttk.Button(master, bg='white', command=back, text='Back', font=('Freestyle Script', 15))
canvas.create_window(100, 575, width=40, height=20, window=btn)
def textvariable(text): # translation of english pop up when buttons pressed
entry.delete(0,END)
entry.insert(0,text)
return
b1=Button(canvas, text="Good morning.", command=lambda:textvariable("Buenos dias.")).grid(row=1, column=1, pady=(100,50), padx=100)
b2=Button(canvas, text="Good night.", command=lambda:textvariable("Buenas noches.")).grid(row=1, column=2, pady=(100,50), padx=100)
b3=Button(canvas, text="Goodbye.", command=lambda:textvariable("Adiós.")).grid(row=2, column=1, pady=50, padx=100)
b4=Button(canvas, text="Hello.", command=lambda:textvariable("Hola.")).grid(row=2, column=2, pady=50, padx=100)
b5=Button(canvas, text="What is your name?", command=lambda:textvariable("¿Cómo te llamas?")).grid(row=3, column=1, pady=50, padx=100)
b6=Button(canvas, text="How are you?", command=lambda:textvariable("¿Cómo estás?")).grid(row=3, column=2, pady=50, padx=100)
b7=Button(canvas, text="Please.", command=lambda:textvariable("Por favor.")).grid(row=4, column=1, pady=50, padx=100)
b8=Button(canvas, text="Thank you.", command=lambda:textvariable("Gracias.")).grid(row=4, column=2, pady=50, padx=100)
entry = Entry(canvas, textvariable=textvariable, width=30)
entry.grid(column=2, row=8, padx=10, pady=(10, 50))
canvas.create_text(295, 570, text = 'Translation:', font = ('Freestyle Script', 25), fill = 'firebrick')
btnStart = ttk.Button(master, bg='white', command=nextScreen, text='Start', font=('Freestyle Script', 35))
canvas.create_window(295, 400, width=400, height=90, window=btnStart)
def back(): # i'm not sure how to make back button or make this work!
canvas.delete("all")
mainloop()
我正在开发一个西班牙语应用程序。我想知道如何有效地创建后退按钮或删除画布上的所有内容,包括小部件。我看到一些帖子创建了一个类,并使用 self. 等等,但我不熟悉所有这些东西。我是初学者。
在第一页中,您将小部件放置在画布上:
canvas.create_window(..., window=btn)
在 wordStudy 中,您正在使用网格:
b1=Button(canvas, text=... )).grid(row=1,...
要删除放置在网格中的小部件,请使用以下任一方法:
b1.grid_remove()
b1.grid_forget()
我不记得确切的区别,所以你必须谷歌它。
回复评论
当您从函数中的画布中删除所有窗口时nextScreen()
,它会起作用,因为画布是在主脚本中定义的。根据名称范围的规则,如果函数未在函数内部定义,则函数将使用 main 中的变量。由于画布是可变的,您可以在不将其声明为全局的情况下对其进行更改。
按钮 b1-b8 在函数内部创建wordStudy()
,引用(名称:b1、b2 等)在函数退出时被垃圾收集。为了将它们声明为全局工作,它们必须在函数外部的主脚本中创建;global 语句所做的就是将主脚本中的变量包含到函数作用域中。
我建议您使用与nextScreen()
整个脚本中的函数相同的方法将小部件放在画布上,因为它工作得很好。如果您始终以相同的方式解决相同的任务,那么代码也更易于阅读和维护。
如果你想尝试grid_forget()
这里有一些示例代码可以玩。做的时候一定要注意范围。
from tkinter import *
root = Tk()
root.geometry('200x100')
field = Canvas(root, bg='white')
field.pack(expand='yes', fill='both')
def start_function(event=None):
print('Start button pressed.')
start_button = Button(field, text='start', command=start_function)
start_button.grid(row=0, column=0) # Place the button on the canvas
def remove_button(event=None): # Forget the button
start_button.grid_forget()
def show_button(event=None): # Remember the button
start_button.grid()
root.bind('a', remove_button) # Press "a" on the keyboard to forget
root.bind('s', show_button) # Press "s" on the keyboard to remember
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句