Python Forum

Full Version: Tkinter: increasing numbers and Radiobutton issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I'm having a bit of trouble with my code using tkinter:

from openpyxl import *
from tkinter import *
from datetime import datetime
from dateutil.relativedelta import relativedelta


wb = load_workbook('C:\\Users\\MyPC\\Desktop\\fs\\excel12.xlsx')

sheet = wb.active
datetimeFormat = '%d-%m-%Y'
class Mainframe(Frame):

    def __init__(self,master,*args,**kwargs):
        global msg, inv_cnt
        excel()
        inv_cnt=1



        Frame.__init__(self,master,*args,**kwargs)
    
        first_name = Label(self, text="First name:", font=('times', 12),anchor='w').place(relx=0.07, rely=0.043, height=15, width=100)
        gender = Label(self, text="Gender:", font=('times', 12),anchor='w').place(relx=0.07, rely=0.123, height=15, width=100)
        info = Label(self, text="Birth Place:", font=('times', 12),anchor='w').place(relx=0.07, rely=0.203, height=15, width=100) 
        code = Label(self, text="Code:", font=('times', 12),anchor='w').place(relx=0.07, rely=0.443, height=15, width=100)

        self.first_name = StringVar(self)
        self.first_name.set('')
        self.first_name=Entry(self,textvariable = self.first_name, width=280, bg='WHITE')
        self.first_name.place(relx=0.18, rely=0.04, height=25, width=160)
      
        option_gender = StringVar()
       #option_gender.set('')
        self.gender_m =Radiobutton(self, text="Male", value="m", var=option_gender)
        self.gender_f =Radiobutton(self, text="Female", value="f", var=option_gender)
        self.gender_m.place(relx=0.15, rely=0.12, height=25, width=100)
        self.gender_f.place(relx=0.27, rely=0.12, height=25, width=100)
        self.select_gender = option_gender.get()

        option_info = StringVar()
        #self.info.set('')
        self.info_e =Radiobutton(self, text="e", value="e", var=option_info)
        self.info_q =Radiobutton(self, text="q", value="q", var=option_info)
        self.info_w =Radiobutton(self, text="w", value="w", var=option_info)
        self.info_e.place(relx=0.17, rely=0.20, height=25, width=50)
        self.info_q.place(relx=0.25, rely=0.20, height=25, width=50)
        self.info_w.place(relx=0.32, rely=0.20, height=25, width=50)
        self.select_info = option_info.get()


     
        self.Submit = Button(self, text='Submit', fg="White", 
                            bg="Red", command=self.insert)
        self.Submit.place(relx=0.18,rely=0.48,height=25,width=100)

        self.Output = StringVar(self)
        self.Output.set('')
        self.Output1 = Label(self, textvariable=self.Output, font=('times', 10),anchor='e', bg='WHITE').place(relx=0.18, rely=0.44, height=25, width=160)

      #  self.first_name.bind("<Return>", self.focus1)

      # self.select_gender.bind("<Return>", self.focus2)    

  #     self.select_info.bind("<Return>", self.focus3)

       # self.da_3.bind("<Return>", self.focus4)

     

   # def focus1(self, event): 
      #  self.select_gender.focus_set()
               

    # def focus2(self, event): 
     #   self.select_info.focus_set()





    def get_inv_count(self,value):
        global inv_cnt
        inv_cnt = value 


    def clear(self):    
        self.first_name.delete(0, END)
        self.select_gender.delete(0, END)
        self.select_info.delete(0, END) 


    def insert(self):
        print(self.first_name.get(), self.select_gender, self.select_info)
        if (self.first_name.get() == "" and
            self.select_gender.get() == "" and
            self.select_info.get() == ""):

            print("empty input") 

        else: 
            current_row = sheet.max_row 
            current_column = sheet.max_column 

           #sheet.cell(row=current_row + 1, column=1).value = 
            sheet.cell(row=current_row + 1, column=2).value = self.first_name.get()
            sheet.cell(row=current_row + 1, column=3).value = print(self.select_gender)
            sheet.cell(row=current_row + 1, column=4).value = self.select_info 

 
            self.Output.set("{0}.{1}.{2}".format(self.first_name.get()[:2],self.select_gender, self.select_info))
            wb.save('excel12.xlsx') 

            self.first_name.focus_set() 

            self.clear() 

def excel():
    
    sheet.cell(row=1, column=1).value = "#"
    sheet.cell(row=1, column=2).value = "first_name"
    sheet.cell(row=1, column=3).value = "gender"
    sheet.cell(row=1, column=4).value = "info"
    sheet.cell(row=1, column=6).value = "code"


class App(Tk):
    def __init__(self):
        global value
        Tk.__init__(self)
        self.title("fs")
        self.geometry('400x500')
        self.resizable(0,0)
                # create and pack a Mainframe window
        Mainframe(self).place(x=0,y=0,height=880,width=880)
        self.mainloop()
        
App()
I'm having three main issue right now:
first: in the column "#" I'd like to add a number for the people who complete the form so (1, 2, 3, 4...) but I'd like be able to choose from what numbert to start (so 101, 102, 103, 104..)
second: gender and info input are not written in my excel file neither in the code.
third: my script run but return and focus right don't work and I'm afreid it might give me problems in future.
Thank you very much for all the help!
A description of what your program is supposed to do would be helpful.

Where in our code are you writing the excel file? How do you expect to get gender inf0?

Where is the completed form counter?

Why are you using global variables inside class Mainframe? I don't see these variables defined outside the class. Why aren't you using self.msg and self.inv_cnt. Did these get imported from someplace? How would I know?

I would like to run your code but I don't have openpyxl. Usually I just comment out the code I don't need, but when you do things like "from openpyxl import * or from tkinter import *" this more work than I am willing to perform just to satisfy my curiosity. As you write more code you'll learn to hate "import *" also, so you may as well stop using it now.