【问题标题】:How to send information to a ScrolledText from a form in Tkinter with Python 3?如何使用 Python 3 从 Tkinter 中的表单向 ScrolledText 发送信息?
【发布时间】:2019-05-01 23:25:06
【问题描述】:

我在 Tkinter 中创建了一个表单,但是当它被响应时,我需要将信息连接到一个文本框。

假设我选择我住在芝加哥,当我按下“发送”按钮时,如何将该信息发送到滚动文本?

   lblCd= Label(ventana,text="Location:",font= ("Arial", 20), 
   fg="blue", bg="white").place(x=70,y=400)
   combo['values']=("Chicago","NY", "Texas")
   combo.current(1)
   combo.place(x=260, y=400)


    Btn=Button(ventana, text="Send",)
    Btn.place(x=200,y=500)

    txt=scrolledtext.ScrolledText(ventana, width=40, height=10) 
    txt.place(x=260, y= 550)
    txt.insert(INSERT, Nombre)

【问题讨论】:

  • Button(..., command=function_name)

标签: python python-3.x user-interface tkinter


【解决方案1】:

您可以在按下按钮时使用Button(..., command=function_name) 来执行function_name()。该函数可以获取选中的值并插入到滚动文本中。

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.scrolledtext as scrolledtext

def send_data():
    txt.insert('end', combo.get() + '\n')

root = tk.Tk()

label = tk.Label(root, text="Location:")
label.pack()

combo = ttk.Combobox(root, values=("Chicago","NY", "Texas"))
combo.pack()
combo.current(0)

button = tk.Button(root, text="Send", command=send_data)
button.pack()

txt = scrolledtext.ScrolledText(root) 
txt.pack()

root.mainloop()

【讨论】:

  • 谢谢你成功了!我怎么能做同样的事情,但有一个复选框? #Checkbox def movies(): print("Action:%d,\n Comedy:%d" % (var1.get(), var2.get())) var1=IntVar() b=Checkbutton(ventana, text= "动作", variable=var1) b.place(x=200, y= 300) var2=IntVar() a=Checkbutton(ventana, text="Comedy", variable=var2) a.place(x=260, y = 300)
  • 你可以得到var1.get()var2.get()insert()到滚动文本。
猜你喜欢
  • 2011-06-26
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 2011-03-27
  • 2012-09-02
  • 2017-10-30
  • 1970-01-01
  • 2016-02-08
相关资源
最近更新 更多