如何让 Tkinter 树视图复选框设置开/关值和变量等,就像我对 Tkinter 检查按钮所做的那样?

发布于 2025-01-16 21:59:09 字数 2004 浏览 1 评论 0原文

我只是一名患有自闭症和其他学习障碍的软件测试人员,正在努力学习 Python。我非常了解机器人框架。我正在通过反复试验来学习 python,并编写了我的第一个脚本,即测试运行程序。它使用 tkinter 检查按钮来分组运行测试。但我想将测试从组中分离出来,以便我可以单独运行它们,但不会有一个包含 50 个测试的大型 tkinter 窗口。我想使用 tkinter CheckboxTreeview 或您可以推荐的任何其他工具,以在树视图中折叠带有复选框的测试。我了解 tkinter CheckBoxTreeview 基本上是如何工作的,但是如何将每个测试附加到树视图中的每个复选框,以便在选中该框并单击运行时运行?下面是我当前代码的示例。我还没有添加树视图,因为我不知道如何使用它来完成我想要做的事情。谢谢。

#!/usr/bin/python3

from Tkinter import *
import Tkinter
import threading as Thread
import robot
from robot import run_cli
from robot import rebot_cli


root = tk.Tk()
root.geometry("400x300")
info_label = Label(text="Check tests to run")

def threading1():
    th1=threading.Thread(target=option)
    th1.start()

frame1 = LabelFrame(root, width=400, height=400)
frame1.pack()

frame2 = LabelFrame(root, width=400, height=400) 
frame2.pack()


var1 = IntVar()
var2 = IntVar()
var3 = IntVar()

c1 = Checkbutton(frame1, text = "Test1", onvalue=1, offvalue=0, variable = var1)
c1.grid(row = 1, column = 0, sticky = W, columnspan = 6)
c2 = Checkbutton(frame1, text = "Test2", onvalue=2, offvalue=0, variable = var2)
c2.grid(row = 2, column = 0, sticky = W, columnspan = 6)
c3 = Checkbutton(frame1, text = "Test3", onvalue=3, offvalue=0, variable = var3)
c3.grid(row = 3, column = 0, sticky = W, columnspan = 6)

def option():
    if var1.get() ==1:
        rc = robot.run_cli(['-t', 'Test1'])
    if var2.get() ==2:
        rc = robot.run_cli(['-t', 'Test2'])
    if var3.get() ==3:
        rc = robot.run_cli(['-t', 'Test3'])

def stop_all():
    os.system("killall -9 chromedriver")
    os.system("killall -9 chrome")
    pidx = os.getpid()
    cmd1 = "kill" + "" + str(pidx)
    if __name__== "__main__":
        os.system(cmd1)

start = Button(frame2, text="Start", width=10, height=3, bg='green', command=threading1)
stop = Button(frame2, text="Quit", width=10, height=3, bg='red', command=stop_all)

root.mainloop()

这就是我想要的样子: 测试运行程序

I am just a software tester with autism and other learning disabilities that is struggling to learn python. I know robot framework very well. I am learning python through trial and error and wrote my first script that is a Test Runner. It uses tkinter checkbuttons to run tests in groups. But I want to split the test up out of their groups so that I can run them individually, but not have a massive tkinter window full of 50 tests. I would like to use tkinter CheckboxTreeview or anything else you can recommend that collapses the test with checkboxes in a treeview. I undertsand how tkinter CheckBoxTreeview basically works, but how do i attach each test to each checkbox in the treeview so that it runs when the box is checked and I click run? Below is an example of my current code. I have not added the treeview, because I don't get how to use it for what I'm trying to do. Thank you.

#!/usr/bin/python3

from Tkinter import *
import Tkinter
import threading as Thread
import robot
from robot import run_cli
from robot import rebot_cli


root = tk.Tk()
root.geometry("400x300")
info_label = Label(text="Check tests to run")

def threading1():
    th1=threading.Thread(target=option)
    th1.start()

frame1 = LabelFrame(root, width=400, height=400)
frame1.pack()

frame2 = LabelFrame(root, width=400, height=400) 
frame2.pack()


var1 = IntVar()
var2 = IntVar()
var3 = IntVar()

c1 = Checkbutton(frame1, text = "Test1", onvalue=1, offvalue=0, variable = var1)
c1.grid(row = 1, column = 0, sticky = W, columnspan = 6)
c2 = Checkbutton(frame1, text = "Test2", onvalue=2, offvalue=0, variable = var2)
c2.grid(row = 2, column = 0, sticky = W, columnspan = 6)
c3 = Checkbutton(frame1, text = "Test3", onvalue=3, offvalue=0, variable = var3)
c3.grid(row = 3, column = 0, sticky = W, columnspan = 6)

def option():
    if var1.get() ==1:
        rc = robot.run_cli(['-t', 'Test1'])
    if var2.get() ==2:
        rc = robot.run_cli(['-t', 'Test2'])
    if var3.get() ==3:
        rc = robot.run_cli(['-t', 'Test3'])

def stop_all():
    os.system("killall -9 chromedriver")
    os.system("killall -9 chrome")
    pidx = os.getpid()
    cmd1 = "kill" + "" + str(pidx)
    if __name__== "__main__":
        os.system(cmd1)

start = Button(frame2, text="Start", width=10, height=3, bg='green', command=threading1)
stop = Button(frame2, text="Quit", width=10, height=3, bg='red', command=stop_all)

root.mainloop()

This is how i want it to look:
TestRunner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文