如何制作堆叠,左右对齐的文本框?

时间:2018-02-06 22:41:25

标签: python python-3.x tkinter

我正在尝试使用右侧和左侧的文本模拟手机文本对话,但不是彼此相邻。我希望他们看起来像是一个接一个地弹出的消息。

我尝试编写此代码:

import tkinter as tk
from tkinter import *

def text_message(answer1, answer2):
    """This will hopefully show in text boxes text messages back and forth"""

    root = Tk()
    frame = Frame(root, bg='white')
    frame.pack(fill='both', expand='yes')
    root.title("Uganda Project")
    T = Text(frame, height=3, width=35)
    T.pack(side=RIGHT)
    T.insert(END, "Did you take your medicine today?\nin 1= Yes\nin 2= No\n")
    T.configure(fg='white', bg='blue')
    root.mainloop()
    for answer in answer1:
        frame2 = Frame(root, bg='white')
        frame2.pack(fill='both', expand='yes')
        T2 = Text(frame2, height=1, width=5)
        T2.pack(side=LEFT)
        T2.insert(END, answer1)
        T2.configure(fg='black', bg='lightgray')
        if answer==1:
            frame3 = Frame(root, bg='white')
            frame3.pack(fill='both', expand='yes')
            T3 = Text(frame3, height=1, width=12)
            T3.pack(side=RIGHT)
            T3.insert(END, "Good Job!")
            T3.configure(fg='white', bg='blue')
        else:
            frame3 = Frame(root, bg='white')
            frame3.pack(fill='both', expand='yes')
            T3 = Text(frame3, height=3, width=32)
            T3.pack(side=RIGHT)
            T3.insert(END, "1= Did you forget?\nin 2= Did you run out of medicine?\nin 3= Did it have bad side effects?\n")
            T3.configure(fg='white', bg='blue')
    for answer in answer2:
        frame4 = Frame(root, bg='white')
        frame4.pack(fill='both', expand='yes')
        T4 = Text(frame4, height=1, width=5)
        T4.pack(side=LEFT)
        T4.insert(END, answer1)
        T4.configure(fg='black', bg='lightgray')
        if answer==1:
            frame5 = Frame(root, bg='white')
            frame5.pack(fill='both', expand='yes')
            T5 = Text(frame5, height=2, width=36)
            T5.pack(side=RIGHT)
            T5.insert(END, "Take it as your earliest convience.\nin Do Not Take two doses at once!\n")
            T5.configure(fg='white', bg='blue')
        elif answer==2:
            frame5 = Frame(root, bg='white')
            frame5.pack(fill='both', expand='yes')
            T5 = Text(frame5, height=1, width=52)
            T5.pack(side=RIGHT)
            T5.insert(END, "Please contact your doctor or pharmacy for refills.\n")
            T5.configure(fg='white', bg='blue')
        else:
            frame5 = Frame(root, bg='white')
            frame5.pack(fill='both', expand='yes')
            T5 = Text(frame5, height=3, width=35)
            T5.pack(side=RIGHT)
            T5.insert(END, "Please contact your doctor about\nin changing your medications or\nin dealing with your symptoms.\n")
            T5.configure(fg='white', bg='blue')
    text_message(2,3)

1 个答案:

答案 0 :(得分:1)

首先,删除

import tkinter as tk

因为你从不使用它。

其次,删除:

root.mainloop()

并添加:

from tkinter import * # this is here only to show indentation level
...
mainloop()

as 最外层范围内的最后一行行,以便它不再阻止下面的表达式。

第三,将方法的调用移动到脚本的主体。删除:

text_message(2,3)

并提出:

from tkinter import * # this is here only to show indentation level
...
text_message(2,3)

然后,运行您的代码并通过替换以下内容来阅读您可以修复的错误:

for answer in answer1:
...
for answer in answer2:

使用:

for answer in range(answer1):
...
for answer in range(answer2):

瞧,你应该拥有自己想要的东西。

回答一般问题:

  

“如何在左右对齐的情况下显示堆叠的小部件?”

我相信你的方法是将窗口小部件封装在一个扩展和填充的框架中,同时将窗口小部件堆叠到框架中的右边或左边(对齐部分) 是一个很好的方法。 / p>