我怎么能在问题和答案之间有一条线(Tkinter-python)

How can I have a line gap between the question and the answer (Tkinter-python)

基本上,我想要在问题和答案之间留一条线。另外,我怎样才能将答案部分的颜色文本更改为红色(红色)示例如下。

问题 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()

您可以在每个问题字符串的末尾连接两个换行符 ('\n'),以便在文本小部件中留下一行后呈现答案。

请注意 TK DOCS -:

中的这一行

the string we supply can be multi-line as well. To do this, simply embed \n (newline) characters in the string at the appropriate locations.

另请注意,通常使用基于字符串的文本索引是个好主意,因为在某些情况下,浮点数可能不足以 in-order 来描述索引。

my_text.insert('1.0', q_lines[i] + '\n\n')

进一步为了在行间隙后呈现答案,插入答案字符串的文本索引必须是 '3.0' -:

my_text.insert('3.0', a_lines[k])

另请注意,使用 f.close().

关闭文件对象始终是一个好习惯。
f = open(...) # Retrieve a file object.
some_random_string = f.readlines() # Use the file object.
f.close() # Close the file object after it's use is finished.

或者你也可以 with open(...) as f : 并在 with 语句的主体中编写使用文件对象 f 的代码,这将自动关闭文件对象 f,一旦在with体内使用就完成了。

with open(...) as f : # Retrieve a file object named f
    some_random_string = f.readlines() # Use the file object.
# Out of with's body f is automatically closed by the with statement.

完成上述更改后的完整代码如下所示 -:

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()
    f.close()
    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
    g = open("Unit1 answers.txt")
    a_lines = g.readlines()
    g.close()
    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()

OUTPUT(测试假设 q_lines = ['question1', 'question2', 'question3']a_lines = ['answer1', 'answer2', 'answer3'] -:

用于测试输出的代码 -:

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()

注意:

  • 有关文本小部件索引的更多信息,请参阅 this
  • 有关如何处理文件对象的正确指南,请参阅 this