gpt4 book ai didi

python - 需要显示在 3 行而不是 1 行

转载 作者:行者123 更新时间:2023-11-28 19:55:41 26 4
gpt4 key购买 nike

我想要的结果是有一个 python 窗口,其中两个按钮“显示信息”“退出”彼此相邻,并让“显示信息”按钮在 3 个单独的行上显示我的姓名和地址然后在单击“退出”时停止程序。我快到了 - 尽管文本都在一行中。

提前致谢。

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

关于python - 需要显示在 3 行而不是 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24704506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com