运行程序时如何取消选中单选按钮?

问题描述 投票:0回答:1

var = tk.stringvar()似乎对我有用。当我使它失去功能时,一切正常。

def checkbox(var, language, val, row, col):
    def ShowChoice():
        print(val)

    tk.Radiobutton(scrollable_body, text=language, variable=var, relief='ridge', value=val, command=ShowChoice,
                   font=("Helvetica", 14)
                   ).grid(row=row, column=col, sticky='nesw')

以及如何使用xlsx writer将它们打印到excel中?

#  "1. Susidomėjęs" check box    #  "1. "2. Stiprus" check box   #  "3. Pilnas įkvėpimo " check box
checkbox('var1', '1', 1, 2, 1), checkbox('var2', '1', 1, 3, 1), checkbox('var3', '1', 1, 4, 1)
checkbox('var1', '2', 2, 2, 2), checkbox('var2', '2', 2, 3, 2), checkbox('var3', '2', 2, 4, 2)
checkbox('var1', '3', 3, 2, 3), checkbox('var2', '3', 3, 3, 3), checkbox('var3', '3', 3, 4, 3)
checkbox('var1', '4', 4, 2, 4), checkbox('var2', '4', 4, 3, 4), checkbox('var3', '4', 4, 4, 4)
checkbox('var1', '5', 5, 2, 5), checkbox('var2', '5', 5, 3, 5), checkbox('var3', '5', 5, 4, 5)

#  "4. Energingas" check box    #  "1. "5. Judrus" check box   #  "6. Veiklus" check box
checkbox('var4', '1', 1, 5, 1), checkbox('var5', '1', 1, 6, 1), checkbox('var6', '1', 1, 6, 1)
checkbox('var4', '2', 2, 5, 2), checkbox('var5', '2', 2, 6, 2), checkbox('var6', '2', 2, 6, 2)
checkbox('var4', '3', 3, 5, 3), checkbox('var5', '3', 3, 6, 3), checkbox('var6', '3', 3, 6, 3)
checkbox('var4', '4', 4, 5, 4), checkbox('var5', '4', 4, 6, 4), checkbox('var6', '4', 4, 6, 4)
checkbox('var4', '5', 5, 5, 5), checkbox('var5', '5', 5, 6, 5), checkbox('var6', '5', 5, 6, 5)
~~~
python tkinter radio-button xlsxwriter
1个回答
0
投票

完整代码未完成

import tkinter as tk
from tkinter import ttk

import xlsxwriter


class Scrollable(tk.Frame):
    """
       Make a frame scrollable with scrollbar on the right.
       After adding or removing widgets to the scrollable frame,
       call the update() method to refresh the scrollable area.
    """

    def __init__(self, frame, width=16):
        scrollbar = tk.Scrollbar(frame, width=width)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=False)

        self.canvas = tk.Canvas(frame, yscrollcommand=scrollbar.set, )
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        scrollbar.config(command=self.canvas.yview)

        self.canvas.bind('<Configure>', self.__fill_canvas)
        body.pack(fill=tk.BOTH, expand=True)

        tk.Frame.__init__(self, frame)

        self.windows_item = self.canvas.create_window(0, 0, window=self, anchor=tk.NW)

    def __fill_canvas(self, event):
        canvas_width = event.width
        self.canvas.itemconfig(self.windows_item, width=canvas_width)

    def update(self):
        self.update_idletasks()
        self.canvas.config(scrollregion=self.canvas.bbox(self.windows_item))


def add_label(name, row, column):
    return tk.Label(scrollable_body, text=name, width=20, height=2, borderwidth=1, relief="groove",
                    font=("Helvetica", 12), fg="black",
                    bg="white").grid(row=row, column=column, sticky='nesw')


root = tk.Tk()
root.title("Emotion test")
root.geometry('1100x700')
root.iconbitmap(r'emotion test icon.ico')
root.minsize(1100, 700)
root.maxsize(1100, 700)
body = ttk.Frame(root)

body.pack()

scrollable_body = Scrollable(body, width=16)

#   TOP LABEL
tk.Label(scrollable_body,
         text="Šiame puslapyje surašyti žodžiai apibūdina skirtingus jausmus ir emocijas. Perskaitykite kiekvieną "
              "žodį ir "
              "\npažymėkite, kaip dažniausiai jaučiatės, t.y. ne kaip Jūs šiandien jaučiatės, bet kaip jūs "
              "apskritai esate linkęs jaustis.",
         width=11, height=2, font=("Helvetica", 15), fg="black",
         bg="white", ).grid(row=0, column=0, columnspan=6, sticky='nesw')
# LOWER TOP LABEL
tk.Label(scrollable_body,
         text="2. Kiek, interviuojant virtualius vaikų avatarus, pojūtis, kad prieš Jus realūs vaikai?",
         width=11, height=2, font=("Helvetica", 15), fg="black",
         bg="white", ).grid(row=22, column=0, columnspan=6, sticky='nesw')
# LOWEST TOP LABEL
tk.Label(scrollable_body,
         text="3. Jei jautėte kurį nors iš žemiau išvardintų jausmų, pažymėkite labiausiai tinkantį.",
         width=11, height=2, font=("Helvetica", 15), fg="black",
         bg="white", ).grid(row=25, column=0, columnspan=6, sticky='nesw')


# CHECKBOX function
def checkbox(language, val, row, col):
    var = tk.IntVar()
    var.set(1)
    def ShowChoice():
        print(val)

    tk.Radiobutton(scrollable_body, text=language, variable=var, relief='ridge', value=val, command=ShowChoice,
                   font=("Helvetica", 14)
                   ).grid(row=row, column=col, sticky='nesw')


#  "1. Susidomėjęs" check box    #  "1. "2. Stiprus" check box   #  "3. Pilnas įkvėpimo " check box
checkbox('1', 1, 2, 1), checkbox('1', 1, 3, 1), checkbox('1', 1, 4, 1)
checkbox('2', 2, 2, 2), checkbox('2', 2, 3, 2), checkbox('2', 2, 4, 2)
checkbox('3', 3, 2, 3), checkbox('3', 3, 3, 3), checkbox('3', 3, 4, 3)
checkbox('4', 4, 2, 4), checkbox('4', 4, 3, 4), checkbox('4', 4, 4, 4)
checkbox('5', 5, 2, 5), checkbox('5', 5, 3, 5), checkbox('5', 5, 4, 5)

#  "4. Energingas" check box    #  "1. "5. Judrus" check box   #  "6. Veiklus" check box
checkbox('1', 1, 5, 1), checkbox('1', 1, 6, 1), checkbox('1', 1, 6, 1)
checkbox('2', 2, 5, 2), checkbox('2', 2, 6, 2), checkbox('2', 2, 6, 2)
checkbox('3', 3, 5, 3), checkbox('3', 3, 6, 3), checkbox('3', 3, 6, 3)
checkbox('4', 4, 5, 4), checkbox('4', 4, 6, 4), checkbox('4', 4, 6, 4)
checkbox('5', 5, 5, 5), checkbox('5', 5, 6, 5), checkbox('5', 5, 6, 5)

#  "7. Išdidus" check box    #  "1. "8. Entuziastingas" check box   #  "9. Ryžtingas" check box
checkbox('1', 1, 7, 1), checkbox('1', 1, 8, 1), checkbox('1', 1, 9, 1)
checkbox('2', 2, 7, 2), checkbox('2', 2, 8, 2), checkbox('2', 2, 9, 2)
checkbox('3', 3, 7, 3), checkbox('3', 3, 8, 3), checkbox('3', 3, 9, 3)
checkbox('4', 4, 7, 4), checkbox('4', 4, 8, 4), checkbox('4', 4, 9, 4)
checkbox('5', 5, 7, 5), checkbox('5', 5, 8, 5), checkbox('5', 5, 9, 5)

#  "10. Atidus" check box    #  "11. Bijantis" check box   #  "12. Prislėgtas" check box
checkbox('1', 1, 10, 1), checkbox('1', 1, 11, 1), checkbox('1', 1, 12, 1)
checkbox('2', 2, 10, 2), checkbox('2', 2, 11, 2), checkbox('2', 2, 12, 2)
checkbox('3', 3, 10, 3), checkbox('3', 3, 11, 3), checkbox('3', 3, 12, 3)
checkbox('4', 4, 10, 4), checkbox('4', 4, 11, 4), checkbox('4', 4, 12, 4)
checkbox('5', 5, 10, 5), checkbox('5', 5, 11, 5), checkbox('5', 5, 12, 5)

#  "13. Kaltas" check box    #  "14. Neramus" check box   #  "15. Kenčiantis" check box
checkbox('1', 1, 13, 1), checkbox('1', 1, 14, 1), checkbox('1', 1, 15, 1)
checkbox('2', 2, 13, 2), checkbox('2', 2, 14, 2), checkbox('2', 2, 15, 2)
checkbox('3', 3, 13, 3), checkbox('3', 3, 14, 3), checkbox('3', 3, 15, 3)
checkbox('4', 4, 13, 4), checkbox('4', 4, 14, 4), checkbox('4', 4, 15, 4)
checkbox('5', 5, 13, 5), checkbox('5', 5, 14, 5), checkbox('5', 5, 15, 5)

#  "16. Nervingas" check box    #  "17. Priešiškas" check box   #  "18. Susigėdęs" check box
checkbox('1', 1, 16, 1), checkbox('1', 1, 17, 1), checkbox('1', 1, 18, 1)
checkbox('2', 2, 16, 2), checkbox('2', 2, 17, 2), checkbox('2', 2, 18, 2)
checkbox('3', 3, 16, 3), checkbox('3', 3, 17, 3), checkbox('3', 3, 18, 3)
checkbox('4', 4, 16, 4), checkbox('4', 4, 17, 4), checkbox('4', 4, 18, 4)
checkbox('5', 5, 16, 5), checkbox('5', 5, 17, 5), checkbox('5', 5, 18, 5)

#  "19. Irzlus" check box    #  "20. Išsigandęs" check box   #  "Realumo pojūtis" check box
checkbox('1', 1, 19, 1), checkbox('1', 1, 20, 1), checkbox('1', 1, 24, 1)
checkbox('2', 2, 19, 2), checkbox('2', 2, 20, 2), checkbox('2', 2, 24, 2)
checkbox('3', 3, 19, 3), checkbox('3', 3, 20, 3), checkbox('3', 3, 24, 3)
checkbox('4', 4, 19, 4), checkbox('4', 4, 20, 4), checkbox('4', 4, 24, 4)
checkbox('5', 5, 19, 5), checkbox('5', 5, 20, 5), checkbox('5', 5, 24, 5)

#  "Liūdesys" check box    #  "Pyktis" check box   #  "Pasišlykštėjimas" check box
checkbox('1', 1, 27, 1), checkbox('1', 1, 28, 1), checkbox('1', 1, 29, 1)
checkbox('2', 2, 27, 2), checkbox('2', 2, 28, 2), checkbox('2', 2, 29, 2)
checkbox('3', 3, 27, 3), checkbox('3', 3, 28, 3), checkbox('3', 3, 29, 3)
checkbox('4', 4, 27, 4), checkbox('4', 4, 28, 4), checkbox('4', 4, 29, 4)
checkbox('5', 5, 27, 5), checkbox('5', 5, 28, 5), checkbox('5', 5, 29, 5)

#  "Nuostaba" check box    #  "Palengvėjimas" check box
checkbox('1', 1, 30, 1), checkbox('1', 1, 31, 1)
checkbox('2', 2, 30, 2), checkbox('2', 2, 31, 2)
checkbox('3', 3, 30, 3), checkbox('3', 3, 31, 3)
checkbox('4', 4, 30, 4), checkbox('4', 4, 31, 4)
checkbox('5', 5, 30, 5), checkbox('5', 5, 31, 5)
# empty square
add_label("", 1, 0)
# lover empty square
add_label("", 23, 0)
# lowest empty square
add_label("", 26, 0)

# 5 top Choice Descriptions

add_label("Visiškai ne arba labai\nmažai", 1, 1)
add_label("Nelabai", 1, 2)
add_label("Vidutiniškai ", 1, 3)
add_label("Gana labai", 1, 4)
add_label("Labai daug", 1, 5)
# 5 LOWER Choice Descriptions
add_label("Visiškai ne arba labai\nmažai", 23, 1)
add_label("Nelabai", 23, 2)
add_label("Vidutiniškai ", 23, 3)
add_label("Gana labai", 23, 4)
add_label("Labai daug", 23, 5)
# 5 LOWEST Choice Descriptions
add_label("Visiškai ne arba labai\nmažai", 26, 1)
add_label("Nelabai", 26, 2)
add_label("Vidutiniškai ", 26, 3)
add_label("Gana labai", 26, 4)
add_label("Labai daug", 26, 5)

# top side labels
add_label("1. Susidomėjęs", 2, 0)
add_label("2. Stiprus", 3, 0)
add_label("3. Pilnas įkvėpimo ", 4, 0)
add_label("4. Energingas", 5, 0)
add_label("5. Judrus", 6, 0)
add_label("6. Veiklus", 7, 0)
add_label("7. Išdidus", 8, 0)
add_label("8. Entuziastingas", 9, 0)
add_label("9. Ryžtingas", 10, 0)
add_label("10. Atidus", 11, 0)
add_label("11. Bijantis", 12, 0)
add_label("12. Prislėgtas", 13, 0)
add_label("13. Kaltas", 14, 0)
add_label("14. Neramus", 15, 0)
add_label("15. Kenčiantis", 16, 0)
add_label("16. Nervingas", 17, 0)
add_label("17. Priešiškas", 18, 0)
add_label("18. Susigėdęs", 19, 0)
add_label("19. Irzlus", 20, 0)
add_label("20. Išsigandęs", 21, 0)
# lower side labels
add_label("Realumo pojūtis", 24, 0)
# lowest side labels
add_label("Liūdesys", 27, 0)
add_label("Pyktis", 28, 0)
add_label("Pasišlykštėjimas", 29, 0)
add_label("Nuostaba", 30, 0)
add_label("Palengvėjimas", 31, 0)

workbook = xlsxwriter.Workbook("Emotion_test1.xlsx")
worksheet1 = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
# CHECKBOXES
# worksheet1.write(1, 2, ch11)

# top side labels
worksheet1.write(1, 0, "1. Susidomėjęs", bold)
worksheet1.set_column(1, 0, 20)
worksheet1.write(2, 0, "2. Stiprus", bold)
worksheet1.write(3, 0, "3. Pilnas įkvėpimo ", bold)
worksheet1.write(4, 0, "4. Energingas", bold)
worksheet1.write(5, 0, "5. Judrus", bold)
worksheet1.write(6, 0, "6. Veiklus", bold)
worksheet1.write(7, 0, "7. Išdidus", bold)
worksheet1.write(8, 0, "8. Entuziastingas", bold)
worksheet1.write(9, 0, "9. Ryžtingas", bold)
worksheet1.write(10, 0, "10. Atidus", bold)
worksheet1.write(11, 0, "11. Bijantis", bold)
worksheet1.write(12, 0, "12. Prislėgtas", bold)
worksheet1.write(13, 0, "13. Kaltas", bold)
worksheet1.write(14, 0, "14. Neramus", bold)
worksheet1.write(15, 0, "15. Kenčiantis", bold)
worksheet1.write(16, 0, "16. Nervingas", bold)
worksheet1.write(17, 0, "17. Priešiškas", bold)
worksheet1.write(18, 0, "18. Susigėdęs", bold)
worksheet1.write(19, 0, "19. Irzlus", bold)
worksheet1.write(20, 0, "20. Išsigandęs", bold)
worksheet1.write(21, 0, "Realumo pojūtis", bold)
worksheet1.write(22, 0, "Liūdesys", bold)
worksheet1.write(23, 0, "Pyktis", bold)
worksheet1.write(24, 0, "Pasišlykštėjimas", bold)
worksheet1.write(25, 0, "Nuostaba", bold)
worksheet1.write(26, 0, "Palengvėjimas", bold)

workbook.close()
scrollable_body.update()

root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.