cuốn sách gpt4 ai đã làm

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

In lại Tác giả: Walker 123 更新时间:2023-11-28 19:55:41 26 4
mua khóa gpt4 Nike

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

Cảm ơn trước.

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

câu trả lời hay nhất

您定义的 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
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress