【问题标题】:How to convert the raw_input text in Python to a label in Tkinter?如何将 Python 中的 raw_input 文本转换为 Tkinter 中的标签?
【发布时间】:2015-07-17 14:47:45
【问题描述】:

我正在尝试为一家名为 Neelam 的餐厅创建此程序。这是一个示例

from Tkinter import *
root=Tk()
w=Label(root,text="**WELCOME TO NEELAM**")#THIS COMES AS A LABEL
w.pack()
s=Label(root,text="*FINE DINE RESTAURANT*")#SO DOES THIS ONE
s.pack()
category=raw_input('BF-BREAKFAST S-SNACKS\n')#HOW TO MAKE THIS ONE ?
q=input('Enter Quantity\n')

【问题讨论】:

  • 您能否明确说明您的需求?
  • 您通常不会在 GUI 程序中使用 raw_input

标签: python python-2.7 user-interface tkinter raw-input


【解决方案1】:

您可以使用其他要求数量的条目。 正如 Bryan 在 cmets 上所说,您不应在 gui 编程中使用 raw_input。

您可以像这样使用条目:

from Tkinter import *

def callback(sv):
    print sv.get()
top = Tk()
L1 = Label(top, text="quantity")
L1.pack( side = LEFT)
sv = StringVar()
sv.trace("w", lambda name, index, mode, sv=sv: callback(sv))
E1 = Entry(top, bd =5, textvariable=sv)
E1.pack(side = RIGHT)
top.mainloop()

编辑:

添加了文本更改事件,用于捕获条目内的用户输入。

【讨论】:

    【解决方案2】:
    from Tkinter import *
    
    root = Tk()
    welcome = Label(root, text="**Welcome to Neelam**")
    welcome.pack()
    
    s = Label(root, text="*Fine Dine Restaurant*")
    s.pack()
    
    # create variables to hold values from the entry
    foodtype = StringVar()
    quantity = StringVar()
    
    
    # create the entry and its label
    foodTypeLabel = Label(root, text="Breakfast(BF) or Snacks(S)")
    foodTypeLabel.pack()
    
    # textvariable option referring back to StringVar() instance
    foodTypeEntry = Entry(root, textvariable=foodtype)
    foodTypeEntry.pack()
    
    quantityLabel = Label(root, text="Quantity:")
    quantityEntry = Entry(root, textvariable=quantity)
    
    quantityLabel.pack()
    quantityEntry.pack()
    
    # create labels that use text from the variable holding
    # the values typed into the Entry widgets
    enteredFood = Label(root, textvariable=foodtype)
    enteredQuant = Label(root, textvariable=quantity)
    enteredFood.pack()
    enteredQuant.pack()
    
    root.mainloop()
    

    【讨论】:

    • 对于模仿形式的 gui,最好使用网格几何管理器
    • 这个站点有很多关于 tkinter 的信息。 nmt.edu/tcc/help/pubs/tkinter。我经常使用它
    猜你喜欢
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多