2021年4月7日星期三

tkinter - How can I standardize Radiobutton list with variable?

I need to create 2x list of Radiobuttons. I wrote then 2 blocks, but I was wondering if I cannot create a function to standardize the structure of the Radiobuttons lists and just call the function by changing the variables.

Current code (it works perfectly):

import os  from tkinter import *    printtype_list=["Externe","Interne 1","Interne 2"]  language_list=["French","English","German","Spanish","Dutch","Russian"]  Language_label="Languages : "  calculation_label="Calculations type : "  i_printtype_list=0  i_language_list=0    def printtype_choose():      global printtype_click      printtype=var_printtype.get()      if printtype=="Externe":          printtype_click="uc_Kalkulation"      elif printtype=="Interne 1":          printtype_click="uc_Kalkulation1"      else :          printtype_click="uc_Kalkulation2"      print(printtype_click)    def language_choose():      global language_click      language=var_language.get()      if language=="French":          language_click="Uc_1"      else :          language_click="Uc_2"      print(language_click)    fenetre=Tk()  fenetre.geometry("800x635")    #----- Beginning of the part to standardize    cadreprinttype=Frame(fenetre)  cadreprinttype.pack(side=TOP,anchor=NW)  printtype_txt=Label(cadreprinttype,text="Calculation type : ",)  printtype_txt.pack(side=LEFT)  var_printtype=StringVar()    for y in range(len(printtype_list)):      Button_printtype=Radiobutton(cadreprinttype, text=printtype_list[i_printtype_list],                                    value=printtype_list[i_printtype_list],                                    variable=var_printtype, command=printtype_choose)      Button_printtype.pack(side=LEFT)      i_printtype_list+=1        cadrelanguage=Frame(fenetre)  cadrelanguage.pack(side=TOP,anchor=NW)  language_txt=Label(cadrelanguage,text="Languages : ")  language_txt.pack(side=LEFT)  var_language=StringVar()    for y in range(len(language_list)):      Button_language=Radiobutton(cadrelanguage, text=language_list[i_language_list],                                   value=language_list[i_language_list],                                   variable=var_language, command=language_choose)      Button_language.pack(side=LEFT)      i_language_list+=1    #----------- End of the part to standardize    fenetre.mainloop()  os.system("pause")  

I tried with this function but without success.

def radio_button(cc,xx,yy,vv):      cadre_1=Frame(fenetre)      cadre_1.pack(side=TOP,anchor=NW)      label_txt=Label(cadre_1,text=cc)      label_txt.pack(side=LEFT)      var=StringVar()        for y in range(len(xx)):          Button_1=Radiobutton(cadre_1,text=xx[yy], value=xx[yy], variable=var, command=vv)          Button_1.pack(side=LEFT)          yy+=1        return;    radio_button(cc=calculation_label, xx=printtype_list, yy=i_printtype_list,               vv=printtype_choose())  
https://stackoverflow.com/questions/66994216/tkinter-how-can-i-standardize-radiobutton-list-with-variable April 08, 2021 at 05:17AM

没有评论:

发表评论