为了账号安全,请及时绑定邮箱和手机立即绑定

如何制作Tkinter条目小部件?

如何制作Tkinter条目小部件?

莫回无 2021-03-13 15:11:56
这是我的代码:import randomfrom 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()被注释的部分应该在段落下面打印一个条目小部件,但它会给IndexError: list index out of rangeline错误a.create_text(500,300, text = paralist[x], width = 700, font = "Times", fill = "purple")。如果我使用e而不是a它可以工作,但是会在一个单独的窗口中打开条目小部件。我试图使tkinter条目窗口小部件与文本显示在同一窗口中。有人可以告诉我该怎么做吗?
查看完整描述

2 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

首先,


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

编辑:


我怀疑您的文件可能是问题。这段代码:


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

以此为“ teste.txt”:


test0

test1

test2

test3

test4

test5

test6

test7

test8

test9

test10

对我来说很好。


查看完整回答
反对 回复 2021-03-29
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

import tkinter

window = tkinter. Tk()

window.geometry('200x200')

ent= tkinter.Entry(window)

ent.pack()

这是在tkinter中最简单的方法。如果您还需要其他东西,请问我:)


查看完整回答
反对 回复 2021-03-29
  • 2 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信