Python 需要在3行而不是1行上显示
我想要的结果是有一个python窗口,其中两个按钮“Show Info”“Quit”相邻,并让“Show Info”按钮在3行单独显示我的姓名和地址 然后在单击“退出”时停止程序。我就快到了,不过课文都在一行上 提前谢谢Python 需要在3行而不是1行上显示,python,tkinter,Python,Tkinter,我想要的结果是有一个python窗口,其中两个按钮“Show Info”“Quit”相邻,并让“Show Info”按钮在3行单独显示我的姓名和地址 然后在单击“退出”时停止程序。我就快到了,不过课文都在一行上 提前谢谢 # This program will demonstrate a button widget within a dialog box # that shows my information. # Call TK interface import tkinter # Call
# This program will demonstrate a button widget within a dialog box
# that shows my information.
# Call TK interface
import tkinter
# Call message box
import tkinter.messagebox
# Create class for GUI.
class MyGUI:
def __init__(self):
#Create the main window widget.
self.main_window = tkinter.Tk()
#Create button widget 1.
self.my_button = tkinter.Button(self.main_window, \
text='Show Info', \
command=self.show_info)
#Creat a quit button.
self.quit_button = tkinter.Button(self.main_window, \
text='Quit', \
command=self.main_window.destroy)
# Pack the buttons.
self.my_button.pack(side='left')
self.quit_button.pack(side='left')
#Enter the tkinter main loop.
tkinter.mainloop()
#The do_somethings will be defined.
def show_info(self):
tkinter.messagebox.showinfo('Text', \
'My Name'
'123 Any Rd'
'Town Fl, 12345')
my_gui = MyGUI()
你定义的3行的换行符并不是你想的那样。。。Python会将这些字符串粉碎在一起,就好像根本没有返回(这就是您所看到的)。相反,试着把这个放在那里:
def show_info(self):
lines = ['My Name', '123 Any Rd', 'Town Fl, 12345']
tkinter.messagebox.showinfo('Text', "\n".join(lines))
只需在infotext中添加一些换行符(\n)。仅在多行上书写并不能使其成为多行文本。这看起来可能很简单,但对于这样几行,最优雅的解决方案是在每行末尾加一个换行符(
\n
):
如果有很多行(例如段落),可以使用:
在文本中添加
\n
tkinter.messagebox.showinfo('Text','My Name\n123 Any Rd\nTown Fl, 12345')
这非常有效-非常感谢您的及时回复。
def show_info(self):
tkinter.messagebox.showinfo('Text', '''\
My Name
123 Any Rd
Town Fl, 12345''')
tkinter.messagebox.showinfo('Text','My Name\n123 Any Rd\nTown Fl, 12345')