小编Sae*_*yth的帖子

3 python中ttk treeviews的不同问题

我正在使用treeviewPython中的多列进行聊天客户端.

这是以下代码treeview:

chat = ttk.Treeview(height="26", columns=("Nick","Mensaje","Hora"), selectmode="extended")
chat.heading('#1', text='Nick', anchor=W)
chat.heading('#2', text='Mensaje', anchor=W)
chat.heading('#3', text='Hora', anchor=W)
chat.column('#1', stretch=NO, minwidth=0, width=130)
chat.column('#2', stretch=NO, minwidth=0, width=620)
chat.column('#3', stretch=NO, minwidth=0, width=65)
chat.column('#0', stretch=NO, minwidth=0, width=0) #width 0 to not display it
Run Code Online (Sandbox Code Playgroud)

我添加这样的项目:

chat.insert("", "end", "", values=((user, message, time)), tags=(messageid))
chat.tag_configure(messageid, foreground='#ff0000')
Run Code Online (Sandbox Code Playgroud)

现在,这完美地运作(这是一个截图作为例子): 截图但最后一行代码会更改该行中所有3列的颜色.我想要的只是改变#2列(只是消息)的文本颜色,而不是整行(不是尼克或时间列).我现在尝试了很长时间但是凌晨4点我投降了☹有没有办法做到这一点?

2周后更新

现在我尝试做3个不同的树视图(每个1列),最终以这种方式结束: 虽然这解决了颜色问题,但我有一个新问题:滚动条.有没有办法将滚动条绑定到3个不同的树视图?到目前为止我所有的尝试都失败了,我只能用滚动条移动一个树视图.可以绑定3个树视图?(如果是的话:怎么样?值得?,我应该吗?)

另外还有一个问题:在TTK python中,所有删除treeview边框的尝试都失败了.

另一个问题是,现在Mensaje树视图只显示第一个单词.不知道为什么没有:\这是关于第一个单词问题的新代码.

chat2 = ttk.Treeview(height="28", columns="Mensaje", selectmode="extended")
chat2.heading('#1', text='Mensaje', anchor=CENTER)
chat2.column('#1', stretch=NO, minwidth=400, width=620)
chat2.column('#0', stretch=NO, minwidth=0, width=0)
Run Code Online (Sandbox Code Playgroud)

这就是消息:

BotGUI.chat2.insert("", …
Run Code Online (Sandbox Code Playgroud)

python treeview ttk

10
推荐指数
2
解决办法
1万
查看次数

如何保持python 3脚本(Bot)运行

(不是母语英语,对不起可能是英语不好.我也是编程的新手).
您好,我正在尝试使用QueryServer连接到TeamSpeak服务器来制作机器人.经过几天的挣扎......它有效,只有一个问题,我坚持使用那一个.

如果您需要检查,这是我正在使用的TeamSpeak API:http://py-ts3.readthedocs.org/en/latest/api/query.html

这是我的脚本中实际发生的事情的摘要:

  1. 它连接起来.
  2. 它检查通道ID(以及它自己的客户端ID)
  3. 它加入了频道
  4. 脚本结束,因此断开连接.

我的问题是:如何让它不断开?如何让脚本保持"等待"状态,以便在有人在频道中键入"hi bot"时它可以读取?阅读文本和回答它们所需的所有代码似乎都很容易编程,但是我遇到了一个问题,我无法让机器人"运行",因为它一旦结束运行脚本就会关闭文件.

更多信息:
我使用的是Python 3.4.1.
我试过学习Threading http://www.tutorialspoint.com/python/python_multithreading.htm,但要么是哑巴,要么它不会像我那样工作.
在API中有一个名为的函数on_event,我希望它一直在运行.机器人代码应该只运行一次然后保持"等待"直到事件发生.我该怎么做?没有线索.

码:

import ts3
import telnetlib
import time

class BotPrincipal:
    def Conectar(ts3conn):
        MiID = [i["client_id"] for i in ts3conn.whoami()]
        ChannelToJoin = "[Pruebas] Bots"
        ts3conn.on_event = BotPrincipal.EventHappened()
        try: 
            BuscandoIDCanal = ts3conn.channelfind(pattern=ChannelToJoin) 
            IDCanal = [i["cid"] for i in BuscandoIDCanal]
            if not IDCanal: 
                print("No channel found with that name")
                return None
            else:
                MiID = str(MiID).replace("'", "")
                MiID = str(MiID).replace("]", …
Run Code Online (Sandbox Code Playgroud)

python api bots

5
推荐指数
1
解决办法
2312
查看次数

调用类函数而不触发__init__

有没有办法在类中调用函数而不触发该类的__init__一部分?假设我有下一个类,通常我会这样调用函数:

class Potato():
    def __init__(self):
        print("Initializing")
    def myfunction(self):
        print("I do something")

Potato().myfunction()
Run Code Online (Sandbox Code Playgroud)

但正如预期的那样,打印出Initializing部分.现在,如果我想调用myfunction而不触发它.你会怎么做?这样做的优点和缺点?它甚至可能吗?

python class python-3.x

5
推荐指数
2
解决办法
3376
查看次数

是否可以使用 Python 编辑 MS word doc 文件?

我需要创建很多旧的 .doc 文件(MS word 97),我想知道在 Python 3 中是否有任何方法可以做。我一直在研究,但我能找到的只是更新的 .docx 的库文件或使用 OpenOffice API。

输出文件将只是一个具有特定格式的 .doc 文件和一些在单元格内带有文本的表格。不需要图像或花哨的东西,但我对安装像 OpenOffice 这样的外部程序犹豫不决,因为这个脚本需要在几台不同的计算机上运行。所以我想知道是否只有一个可以导入的库。

我也接受其他编程语言的建议,但 Python 会是完美的。

python

4
推荐指数
1
解决办法
3518
查看次数

减少代码行数而不影响性能

我还在学习Python(3.5),我有这段代码,需要为一些对象分配很多值,所以我想知道什么是最优化的方法.我问这个是因为我缺乏知识来理解循环中"较少的代码行"是否比不使用循环更耗时(文件大小较小,但是proccesing会增加......也许?) .

方法1:

    # 01
    self.lineReferencia1.setText(utilidades.ensure_its_str(registro[16]))
    self.lineConcepto1.setText(utilidades.ensure_its_str(registro[17]))
    self.lineCantidad1.setText(utilidades.ensure_its_str(registro[18]))
    self.linePrecio1.setText(utilidades.ensure_its_str(registro[19]))
    self.lineDescuento1.setText(utilidades.ensure_its_str(registro[20]))
    # 02
    self.lineReferencia2.setText(utilidades.ensure_its_str(registro[23]))
    self.lineConcepto2.setText(utilidades.ensure_its_str(registro[24]))
    self.lineCantidad2.setText(utilidades.ensure_its_str(registro[25]))
    self.linePrecio2.setText(utilidades.ensure_its_str(registro[26]))
    self.lineDescuento2.setText(utilidades.ensure_its_str(registro[27]))
    # 03
    self.lineReferencia3.setText(utilidades.ensure_its_str(registro[30]))
    self.lineConcepto3.setText(utilidades.ensure_its_str(registro[31]))
    self.lineCantidad3.setText(utilidades.ensure_its_str(registro[32]))
    self.linePrecio3.setText(utilidades.ensure_its_str(registro[33]))
    self.lineDescuento3.setText(utilidades.ensure_its_str(registro[34]))
    # 04
    self.lineReferencia4.setText(utilidades.ensure_its_str(registro[37]))
    self.lineConcepto4.setText(utilidades.ensure_its_str(registro[38]))
    self.lineCantidad4.setText(utilidades.ensure_its_str(registro[39]))
    self.linePrecio4.setText(utilidades.ensure_its_str(registro[40]))
    self.lineDescuento4.setText(utilidades.ensure_its_str(registro[41]))
Run Code Online (Sandbox Code Playgroud)

方法2:

    items = (
    (self.lineReferencia1, registro[16]), (self.lineConcepto1, registro[17]), (self.lineCantidad1, registro[18]),
    (self.linePrecio1, registro[19]), (self.lineDescuento1, registro[20]), (self.lineReferencia2, registro[23]),
    (self.lineConcepto2, registro[24]), (self.lineCantidad2, registro[25]), (self.linePrecio2, registro[26]),
    (self.lineDescuento2, registro[27]), (self.lineReferencia3, registro[30]), (self.lineConcepto3, registro[31]),
    (self.lineCantidad3, registro[32]), (self.linePrecio3, registro[33]), (self.lineDescuento3, registro[34]),
    (self.lineReferencia4, registro[37]), (self.lineConcepto4, registro[38]), (self.lineCantidad4, registro[39]),
    (self.linePrecio4, registro[40]), (self.lineDescuento4, registro[41]))
    for i in items:
        i[0].setText(utilidades.ensure_its_str(i[1])) …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

1
推荐指数
1
解决办法
70
查看次数

使用此日期格式反向排序列表的最佳方法是什么?

我有一个包含数百万条记录的数据库,如下所示:

[("Something", "10/08/2017", "something else", "something more", "etc"),
("Something", "16/08/2017", "something else", "something more", "etc"),
("Something", "14/07/2017", "something else", "something more", "etc"),
("Something", "14/08/2017", "something else", "something more", "etc"),
("Something", "15/07/2017", "something else", "something more", "etc"),
("Something", "14/08/2017", "something else", "something more", "etc")]
Run Code Online (Sandbox Code Playgroud)

这是DD/MM/YYYY我需要它按第二个值反转排序.

第一次尝试:
我试过这个,但它只按白天而不是按月分类.

def sort_by(my_list, index):
    my_list.sort(key=lambda x: x[index], reverse=True)
    return my_list
Run Code Online (Sandbox Code Playgroud)

结果的图片(忽略颜色,它来自我的GUI):https://image.prntscr.com/image/ZeIgzxNaQ2OD4VX0Yztnhw.png
正如你所看到的那样,它只在白天进行排序,而不是按月进行排序,所以它保持不变每当一天变化时,从一个月跳到另一个月.那是一团糟.

第二次尝试:

def sort_by_date(my_list, index):
    for record, data in enumerate(my_list):
        split = data[index].split("/")
        altered_record = my_list[record]
        altered_record[index] = split[2]+"-"+split[1]+"-"+split[0]
    my_list.sort(key=lambda x: …
Run Code Online (Sandbox Code Playgroud)

python sorting

1
推荐指数
1
解决办法
90
查看次数

文件中的评估行

我正在创建一个将选项存储在txt文件中的程序,我尝试了几行不同的操作,但到目前为止,我认为an eval是最好的。

我在文件中得到此信息:

colordefondo = "#abcdef"
Run Code Online (Sandbox Code Playgroud)

eval在程序中得到了这个:

for line in open("filename.txt", "r"):
  eval(line)
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试代码时,它给了我这个错误:

Traceback (most recent call last):
File "D:\project-sae\Sae.py", line 25, in <module>
eval(line)
File "<string>", line 1
colordefondo = "#abcdef"
             ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么?而且,如果有人知道从txt文件加载和存储几个选项的值的更好方法,那就太好了。不过,我的问题是为什么eval失败了,我对eval工作原理的了解似乎是错误的,而且我也不知道为什么,以前曾多次使用它,但从未从文件中获取过。

python eval file

0
推荐指数
1
解决办法
1280
查看次数

标签 统计

python ×7

python-3.x ×2

api ×1

bots ×1

class ×1

eval ×1

file ×1

sorting ×1

treeview ×1

ttk ×1