我正在创建三个“消息小部件”,但它们似乎根据内部内容调整宽度和高度的大小,是否可以防止这种情况?
from tkinter import *
root = Tk()
root.configure(background = 'Yellow')
root.geometry('500x400')
a = '{}'.format('The Elepthan is Big')
b = '{}'.format('The Bird')
c = '{}'.format('The Lion is big and ferocious, kills any animal')
msg = Message(root, text = a, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()
msg = Message(root, text = b, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()
msg = Message(root, text = c, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud) 需要帮助,让我们假设我在一个名为'input'的变量中有一个字符串'Sam-Person'
name, kind = input.split('-')
Run Code Online (Sandbox Code Playgroud)
通过以上操作,我得到两个变量,其中不同的字符串'Sam'和'Person'有一种方法只能得到第一个值name ='Sam',而不需要额外的变量'kind'而无需使用名单?这样做时,假设我只会得到'Sam':
name = input.split('-')
Run Code Online (Sandbox Code Playgroud)
我得到一个列表,然后我可以通过索引名[0]或名称[1]访问这些值,但这不是我想要的,我只是想直接将'Sam'变成变量'name',是吗一种方法可以做到这一点或替代分裂?