Python Forum
Python if statement docx wount work - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python if statement docx wount work (/thread-18947.html)



Python if statement docx wount work - Selfiatus1 - Jun-07-2019

Hi,

I cant get this code to work, I just want a button to be pressed, calling definition "helloCallBack" and from it a series of
if statements, whereas, if a variable is a string then write that string to the document..

It just woun't work, what am I doing wrong?

def helloCallBack():

   if tkvar == 'Broker':
      document = Document()
      document.add_heading("Broker")
      document.save('agreement034.docx')
Kr.
Selfiatus1

...and yes I do have installed docx and tkinter modules.

It woun't write text to the document, can anyone help please?


RE: Python if statement docx wount work - Larz60+ - Jun-07-2019

You can't make a button function ('work') if you don't define one first.
Are you showing all pertinent code?


RE: Python if statement docx wount work - Selfiatus1 - Jun-07-2019

Here is the full code:

import tkinter as tk

from docx import Document

HEIGHT = 500
WIDTH = 600

root = tk.Tk()
root.title("Generator")
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

background_image = tk.PhotoImage(file='bg123.png')
background_label = tk.Label(root, image=background_image)
background_label.place(x=-6, y=-6)

#dropdown part
# Create a Tkinter variable
tkvar = tk.StringVar(root)
choices = { 'broker','consultant','recruit'}
tkvar.set('broker') # set the default option
popupMenu = tk.OptionMenu(root, tkvar, *choices)
popupMenu.place(x=260,y=95)

def helloCallBack():
 
   if tkvar == 'Broker':
      document = Document()
      document.add_heading("Broker")
      document.save('0034.docx')

button = tk.Button(text="Generate", font=40, command=helloCallBack)
button.place(x=230, y=380)

root.mainloop()



RE: Python if statement docx wount work - woooee - Jun-07-2019

You don't have a command to call in your OptionMenu so nothing happens when something is selected.


RE: Python if statement docx wount work - Selfiatus1 - Jun-07-2019

(Jun-07-2019, 05:39 PM)woooee Wrote: You don't have a command to call in your OptionMenu so nothing happens when something is selected.

Is it not called when pressing the button? The tkvar variable... if it is some value then the button decides what to do

Here is the code again, the problem is that it is not writing to the document when pressing the button "Generate", just try it yourselves, it wount work but it should work obviously?

import tkinter as tk
 
from docx import Document
 
HEIGHT = 500
WIDTH = 600
 
root = tk.Tk()
root.title("Generator")
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
 
background_image = tk.PhotoImage(file='bg123.png')
background_label = tk.Label(root, image=background_image)
background_label.place(x=-6, y=-6)
 
#dropdown part
# Create a Tkinter variable
tkvar = tk.StringVar(root)
choices = { 'broker','consultant','recruit'}
tkvar.set('broker') # set the default option
popupMenu = tk.OptionMenu(root, tkvar, *choices)
popupMenu.place(x=260,y=95)
 
def helloCallBack():
  
   if tkvar == 'broker':
      document = Document()
      document.add_heading("broker")
      document.save('0034.docx')
 
button = tk.Button(text="Generate", font=40, command=helloCallBack)
button.place(x=230, y=380)
 
root.mainloop()



RE: Python if statement docx wount work - Larz60+ - Jun-07-2019

Add a print statement as the first thing in your 'helloCallBack()' function
a simple:
print('..')
will do. To see if you are at least making it to the function.
You have the command set up properly in your button.