Posts: 5
Threads: 2
Joined: Jun 2019
Jun-07-2019, 03:54 PM
(This post was last modified: Jun-07-2019, 04:30 PM by Selfiatus1.)
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?
1 2 3 4 5 6 |
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?
Posts: 12,043
Threads: 487
Joined: Sep 2016
You can't make a button function ('work') if you don't define one first.
Are you showing all pertinent code?
Posts: 5
Threads: 2
Joined: Jun 2019
Jun-07-2019, 05:35 PM
(This post was last modified: Jun-07-2019, 05:37 PM by Selfiatus1.)
Here is the full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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 )
tkvar = tk.StringVar(root)
choices = { 'broker' , 'consultant' , 'recruit' }
tkvar. set ( 'broker' )
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()
|
Posts: 536
Threads: 0
Joined: Feb 2018
Jun-07-2019, 05:39 PM
(This post was last modified: Jun-07-2019, 05:40 PM by woooee.)
You don't have a command to call in your OptionMenu so nothing happens when something is selected.
Posts: 5
Threads: 2
Joined: Jun 2019
Jun-07-2019, 05:57 PM
(This post was last modified: Jun-07-2019, 05:57 PM by Selfiatus1.)
(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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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 )
tkvar = tk.StringVar(root)
choices = { 'broker' , 'consultant' , 'recruit' }
tkvar. set ( 'broker' )
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()
|
Posts: 12,043
Threads: 487
Joined: Sep 2016
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.
|