小兔狲 发表于 2020-5-26 22:38:54

求助小甲鱼零基础入门python的两个问题

小白卑微发帖 求助两个问题
均出现在 GUI Tkinter7 这个视频中

【视频链接】https://www.bilibili.com/video/BV1Fs411A7HZ?p=71
【问题1】
text.tag_config(cursor)会报错
【出错源代码】
# https://blog.csdn.net/cool99781/article/details/106193125
# from tkinter import *
# import webbrowser
# root= Tk()
# text1= Text(root,width=30,height=20)
# text1.pack()
# # 先创建一个text组件
# text1.insert(INSERT,'I love Hins')
# # 插入text的文本内容
# text1.tag_add("tag1",'1.2','1.5')
# # 1.2,1.5 列上,左闭右开~
# text1.tag_add("tag2",'1.7','1.11')
# # 添加标签
# text1.tag_config('tag1',background='yellow',foreground='red')
# text1.tag_config('tag2',background='black',foreground='white')
# # 给标签设置填充和颜色
# def show_arrow(event):
#   text1.tag_config('tag2',cursor='arrow')
# #   ???????会报错?
# def show_arrow_1(event):
#   text1.tag_config('tag2',cursor='xterm')
# def link(event):
#   webbrowser.open('https://y.qq.com/portal/search.html#page=2&searchid=1&remoteplace=txt.yqq.top&t=lyric&w=%E5%BC%A0%E6%95%AC%E8%BD%A9')
# text1.tag_bind('tag2','<Enter>',show_arrow)
# text1.tag_bind('tag2','<Leave>',show_arrow_1)
# text1.tag_bind('tag2','<Button-1>',link)
# # 定义函数还有绑定事件的命令
【错误】_tkinter.TclError: unknown option "-cursor"


【问题2】 获取搜索内容的index转换语句问题
【问题代码】
from tkinter import *
root=Tk()
text1= Text(root,width=30,height=20)
text1.pack()
# 先创建一个text组件
text1.insert(INSERT,'I love Hins,he is very intelligent and hansome.')
# 设计一个获取位置索引的函数:
def getPo(text,index):
    return tuple(map(int,str.split(text,index(index),'.')))   # 这里,‘.’ 会出现 高亮背景色,出错的意思

# 设计一个查找的while循环:
start='1.0'   #初始查找位置
while True:
    po=text1.search('e',start,stopindex=END)# 获取字母e的位置
    if not po:   #如果没找到e
      break
    print('字母e的位置索引为:',getPo(text1,po))
    start= po +'1c'
mainloop()
【报错】TypeError: 'str' object is not callable

如果有大佬可以弄明白这两个错误 真是感激不尽!!!!!!!

Twilight6 发表于 2020-5-27 08:02:42

问题一:
你代码打错了,用的是config来添加鼠标移动到标签时候的事件 而你用了 tag_config ,改成这样即可:def show_arrow(event):
    text1.config(cursor='arrow')
def show_arrow_1(event):
    text1.config(cursor='xterm')完整代码:from tkinter import *
import webbrowser
root= Tk()
text1= Text(root,width=30,height=20)
text1.pack()
# 先创建一个text组件
text1.insert(INSERT,'I love Hins')
# 插入text的文本内容
text1.tag_add("tag1",'1.2','1.5')
# 1.2,1.5 列上,左闭右开~
text1.tag_add("tag2",'1.7','1.11')
# 添加标签
text1.tag_config('tag1',background='yellow',foreground='red')
text1.tag_config('tag2',background='black',foreground='white')
# 给标签设置填充和颜色
def show_arrow(event):
    text1.config(cursor='arrow')
#   ???????会报错?
def show_arrow_1(event):
    text1.config(cursor='xterm')
def link(event):
    webbrowser.open('https://y.qq.com/portal/search.html#page=2&searchid=1&remoteplace=txt.yqq.top&t=lyric&w=%E5%BC%A0%E6%95%AC%E8%BD%A9')
text1.tag_bind('tag2','<Enter>',show_arrow)
text1.tag_bind('tag2','<Leave>',show_arrow_1)
text1.tag_bind('tag2','<Button-1>',link)


mainloop()

问题二:
你代码打错了,是 str.split(text.index(index), ".") 而不是 str.split(text,index(index),'.')

还有 start= po +'+1c'要带正号

正确代码:from tkinter import *
root=Tk()
text1= Text(root,width=30,height=20)
text1.pack()
# 先创建一个text组件
text1.insert(INSERT,'I love Hins,he is very intelligent and hansome.')
# 设计一个获取位置索引的函数:
def getPo(text,index):
    return tuple(map(int,str.split(text.index(index),'.')))   # 这里,‘.’ 会出现 高亮背景色,出错的意思

# 设计一个查找的while循环:
start='1.0'   #初始查找位置
while True:
    po=text1.search('e',start,stopindex=END)# 获取字母e的位置
    if not po:   #如果没找到e
      break
    print('字母e的位置索引为:',getPo(text1,po))
    start= po +'+1c'
mainloop()

做题要细心哦

zwhe 发表于 2020-5-27 10:09:34

{:5_109:}

小兔狲 发表于 2020-5-28 10:12:28

Twilight6 发表于 2020-5-27 08:02
问题一:
你代码打错了,用的是config来添加鼠标移动到标签时候的事件 而你用了 tag_config ,改成这样即 ...

给大佬跪了~~~感激不尽~~~~~

Twilight6 发表于 2020-5-28 10:15:00

小兔狲 发表于 2020-5-28 10:12
给大佬跪了~~~感激不尽~~~~~

https://xxx.ilovefishc.com/forum/202005/27/132745rjvcvw1z2148jthd.gif

小兔狲 发表于 2020-5-28 10:18:43

Twilight6 发表于 2020-5-27 08:02
问题一:
你代码打错了,用的是config来添加鼠标移动到标签时候的事件 而你用了 tag_config ,改成这样即 ...

ValueError: dictionary update sequence element #0 has length 1; 2 is required
请问大佬 为什么我改成了text1.config()之后 又出现这样的报错{:5_104:}

Twilight6 发表于 2020-5-28 10:20:23

小兔狲 发表于 2020-5-28 10:18
ValueError: dictionary update sequence element #0 has length 1; 2 is required
请问大佬 为什么我改 ...

全部都要改 ,建议直接对照我代码复制吧

liaoyiqin 发表于 2020-5-28 13:43:44

Twilight6 发表于 2020-5-27 08:02
问题一:
你代码打错了,用的是config来添加鼠标移动到标签时候的事件 而你用了 tag_config ,改成这样即 ...

666

liaoyiqin 发表于 2020-5-28 13:44:15

666
页: [1]
查看完整版本: 求助小甲鱼零基础入门python的两个问题