Python Forum
Python if statement docx wount work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python if statement docx wount work
#1
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?
Reply
#2
You can't make a button function ('work') if you don't define one first.
Are you showing all pertinent code?
Reply
#3
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()
Reply
#4
You don't have a command to call in your OptionMenu so nothing happens when something is selected.
Reply
#5
(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()
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  no module named 'docx' when importing docx MaartenRo 1 706 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 2,847 Oct-17-2023, 06:03 PM
Last Post: Devan
  python-docx: preserve formatting when printing lines Tmagpy 4 2,005 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  python-docx- change lowercase to bold, italic Tmagpy 0 1,353 Jul-01-2022, 07:25 AM
Last Post: Tmagpy
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,488 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  python-docx regex: replace any word in docx text Tmagpy 4 2,139 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  getting an import statement to work in my program barryjo 1 1,615 Dec-06-2021, 04:28 PM
Last Post: snippsat
  Сombine (Merge) word documents using python-docx Lancellot 1 11,360 May-12-2021, 11:07 AM
Last Post: toothedsword
  How to add run in paragraph using python-docx? toothedsword 0 2,740 May-12-2021, 10:55 AM
Last Post: toothedsword
  Why doesn't this print statement work? stylingpat 10 5,588 Mar-23-2021, 07:54 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020