基本上,我想要問題和答案之間的差距。另外,我如何更改答案部分的顏色文本(變為紅色)下面的示例。
問題 1
答案 1
from tkinter import *
root = Tk()
root.title("Flashcard Revision")
root.geometry("700x500")
root.config(bg = "#3062C7")
i = 0
k = -1
def question():
clear()
global my_text, f, q_lines, i, k
f = open("Unit1 questions.txt")
q_lines = f.readlines()
my_text.insert(1.0, q_lines[i])
k = k 1
i = i 1
def clear():
my_text.delete(1.0, END)
def answer():
global my_text, f, a_lines, k
g = open("Unit1 answers.txt")
a_lines = g.readlines()
my_text.insert(2.0, a_lines[k])
q_button = Button(root, text="Question", bg="white", activebackground="red", command=question).place(x=285, y=280)
a_button = Button(root, text = "Answer", bg="white", activebackground="red", command=answer).place(x=355, y=280)
my_text = Text(root, width=40, height=10, font=("Helvetica", 16))
my_text.pack(pady=20)
root.mainloop()
uj5u.com熱心網友回復:
'\n'您可以在每個問題字串的末尾連接兩個換行符 ( ) 以在文本小部件中留下一行后呈現答案。
請注意
用于測驗輸出的代碼 -:
from tkinter import *
root = Tk()
root.title("Flashcard Revision")
root.geometry("700x500")
root.config(bg = "#3062C7")
i = 0
k = -1
def question():
clear()
global my_text, f, q_lines, i, k
q_lines = ['question1', 'question2', 'question3']
my_text.insert('1.0', q_lines[i] '\n\n')
k = k 1
i = i 1
def clear():
my_text.delete('1.0', END)
def answer():
global my_text, f, a_lines, k
a_lines = ['answer1', 'answer2', 'answer3']
my_text.insert('3.0', a_lines[k])
q_button = Button(root, text="Question", bg="white", activebackground="red", command=question).place(x=285, y=280)
a_button = Button(root, text = "Answer", bg="white", activebackground="red", command=answer).place(x=355, y=280)
my_text = Text(root, width=40, height=10, font=("Helvetica", 16))
my_text.pack(pady=20)
root.mainloop()
筆記:
- 有關文本小部件索引的更多資訊,請參閱此。
- 有關如何處理檔案物件的正確指南,請參閱此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/439075.html
