简体   繁体   English

如何制作Tkinter条目小部件?

[英]How do I make a tkinter entry widget?

This is my code: 这是我的代码:

import random
from tkinter import *
root=Tk()
a=Canvas(root, width=1000, height=1000)
a.pack()
e = Entry(root)
paralist = []
x=random.randint(1,10)
file = open("groupproject.txt")
line = file.readline()
for line in file:
    paralist.append(line.replace("\n", ""));

a.create_text(500,50, text = "Typing Fun", width = 700, font = "Verdana", fill = "purple")
a.create_text(500,300, text = paralist[x], width = 700, font = "Times", fill = "purple")

#master = Tk()
#a = Entry(master)
#a.pack()
#a.focus_set()
#def callback():
#    print (a.get())


root.mainloop()

The commented section is supposed to print an entry widget underneath a paragraph but instead it gives an error IndexError: list index out of range for line a.create_text(500,300, text = paralist[x], width = 700, font = "Times", fill = "purple") . 带注释的部分应该在段落下打印一个条目小部件,但会出现错误IndexError: list index out of range a.create_text(500,300, text = paralist[x], width = 700, font = "Times", fill = "purple") If I use e instead of a it works but opens the entry widget in a separate window. 如果我使用e ,而不是a它的工作原理,但在打开一个单独的窗口中输入小工具。

I'm trying to make the tkinter entry widget appear in the same window as the text. 我试图使tkinter条目小部件与文本显示在同一窗口中。 Can someone please tell me how to do this? 有人可以告诉我该怎么做吗?

First off, 首先,

paralist = [] The list is empty so a random word between 1 and 10 will be wrong since theres nothing in the list. paralist = []列表为空,因此1到10之间的随机单词将是错误的,因为列表中没有任何内容。

master = Tk() # since Tk() is already assigned to root this will make a new window
a = Entry(master) # a is already assigned to canvas
a.pack() # this is already declare under a=canvas
a.focus_set()
def callback():
    print (a.get())

Edit: 编辑:

I suspect that your file might be the problem. 我怀疑您的文件可能是问题。 This code: 这段代码:

import random
from tkinter import *

root = Tk()

a = Canvas(root, width = 400, height = 400)
a.pack()
e = Entry(root)
e.pack()

paralist = []

x = random.randint(1,10)

file = open("teste.txt")
line = file.readline()

for line in file:
    paralist.append(line.replace("\n", ""));

a.create_text(200,50, text = "Typing Fun", width = 700, font = "Verdana", fill = "purple")
a.create_text(200,300, text = paralist[x], width = 700, font = "Times", fill = "purple")

b = Entry(root)
b.pack()
b.focus_set()

def callback():
    print (a.get()) 

root.mainloop()

With this as "teste.txt": 以此为“ teste.txt”:

test0
test1
test2
test3
test4
test5
test6
test7
test8
test9
test10

Works fine for me. 对我来说很好。

import tkinter
window = tkinter. Tk()
window.geometry('200x200')
ent= tkinter.Entry(window)
ent.pack()

This is the easiest way to do it in tkinter. 这是在tkinter中最简单的方法。 If you need something else Just ask me:) 如果您还需要其他东西,请问我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 Tkinter 小部件中的条目已更正,如何使 tkinter 消息消失? - How do I make a tkinter message disappear if the entry in Tkinter Widget has been corrected? 如何在不增大字体的情况下使 Tkinter 条目小部件更宽? - How do I make a Tkinter entry widget wider without making the font larger? Tkinter:如何隐藏文本,就像使用带有文本(而非输入)小部件的密码一样? - Tkinter: How do I hide text as if with a password with a Text (not Entry) widget? 如何以像素为单位设置 Tkinter Entry 小部件的宽度? - How do I set the width of an Tkinter Entry widget in pixels? 如何在Tkinter中检索Entry小部件的值? - How do I retrieve the value of an Entry widget in Tkinter? Tkinter,如何制作数组入口小部件? - Tkinter, How to make an array entry widget? 如何自动使 python tkinter Entry 自动进行? - How do I automatically make python tkinter Entry automatically? Tkinter,Python:如何保存在“输入”小部件中输入的文本? 如何移动标签? - Tkinter, Python: How do I save text entered in the Entry widget? How do I move a label? 如何使Tkinter条目窗口小部件数据可用于其他功能 - How can I make Tkinter entry widget data made available for other functions 当我在 tkinter 中按浏览按钮时,如何填充条目小部件? - How do I populate the entry widget when I press the Browse button in tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM